Articles → CSHARP → Continuous Input To Switch Case In C#

Continuous Input To Switch Case In C#





using System;

namespace switch_case {
  class Program {
    static void Main(string[] args) {
      Console.WriteLine("Please enter the option...");
      string option = Console.ReadLine();
      int output;

      if (Int32.TryParse(option, out output) == true) {
        while (output != 0) {
          Program p = new Program();
          p.SwitchCaseMethod(output);
          Console.WriteLine("Please enter the option...");
          option = Console.ReadLine();
          Int32.TryParse(option, out output);
        }

        Console.WriteLine("Case 0 executed. Please any key to exit the program");
        Console.ReadLine();
      }
    }

    public void SwitchCaseMethod(int option) {
      switch (option) {
      case 1:
        Console.WriteLine("case 1 executed");
        break;
      case 2:
        Console.WriteLine("case 2 executed");
        break;
      case 3:
        Console.WriteLine("case 3 executed");
        break;
      default:
        Console.WriteLine("case else");
        break;
      }
    }
  }
}



Output


Picture showing how to provide continuous input to switch case in c#
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, April 28, 2015

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250