switch case in C# Programming Language

Rumman Ansari   Software Engineer   2019-07-12   5691 Share
☰ Table of Contents

Table of Content:


The switch.case statement is good for selecting one branch of execution from a set of mutually exclusive ones. It takes the form of a switch argument followed by a series of case clauses. When the expression in the switch argument evaluates to one of the values beside a case clause, the code immediately following the case clause executes. This is one example where you don't need to use curly braces to join statements into blocks; instead, you mark the end of the code for each case using the break statement. You can also include a default case in the switch statement, which will execute if the expression evaluates to none of the other cases. The following switch statement tests the value of the integerA variable:

switch (integerA)
{
   case 1:
      Console.WriteLine("integerA =1");
      break;
   case 2:
      Console.WriteLine("integerA =2");
      break;
   case 3:
      Console.WriteLine("integerA =3");
      break;
   default:
      Console.WriteLine("integerA is not 1,2, or 3");
      break;
}

Note that the case values must be constant expressions; variables are not permitted.

Though the switch.case statement should be familiar to C and C++ programmers, C#'s switch.case is a bit safer than its C++ equivalent. Specifically, it prohibits fall-through conditions in almost all cases. This means that if a case clause is fired early on in the block, later clauses cannot be fired unless you use a goto statement to mark that you want them fired, too. The compiler enforces this restriction by flagging every case clause that is not equipped with a break statement as an error similar to this:

Control cannot fall through from one case label ('case 2:') to another

Although it is true that fall-through behavior is desirable in a limited number of situations, in the vast majority of cases, it is unintended and results in a logical error that's hard to spot. Isn't it better to code for the norm rather than for the exception?

By getting creative with goto statements, however, you can duplicate fall-through functionality in your switch.cases. But, if you find yourself really wanting to, you probably should reconsider your approach. The following code illustrates both how to use goto to simulate fall-through, and how messy the resultant code can get:

// assume country and language are of type string
switch(country)
{
   case "America":
      CallAmericanOnlyMethod();
      goto case "Britain";
   case "France":
      language = "French";
      break;
   case "Britain":
      language = "English";
      break;
}

There is one exception to the no-fall-through rule, however, in that you can fall through from one case to the next if that case is empty. This allows you to treat two or more cases in an identical way (without the need for goto statements):

switch(country)
{
   case "au":
   case "uk":
   case "us":
      language = "English";
      break;
   case "at":
   case "de":
      language = "German";
      break;
}

One intriguing point about the switch statement in C# is that the order of the cases doesn't matter — you can even put the default case first! As a result, no two cases can be the same. This includes different constants that have the same value, so you can't, for example, do this:

// assume country is of type string
const string england = "uk";
const string britain = "uk";
switch(country)
{
   case england:
   case britain:    // This will cause a compilation error.
      language = "English";
      break;
}

The previous code also shows another way in which the switch statement is different in C# compared to C++: In C#, you are allowed to use a string as the variable being tested.

Solve These Problems
  1. Find the minimum and maximum of 2 input int values.
  1. Input a year and check whether its a leap year. If the input year is a leap year, show a message "The given year is a leap year". If the input year is not a leap year, show a message "This is not a leap year".
  1. Develop a program, that accepts a deposit amount and calculates the amount of interest the deposit amount earns in a year. The bank pays a flat 4.0% per year for deposits of up to Rs.1000, a flat 4.5% per year for deposits of up to Rs.5000, and a flat 5.0% per year for deposits of more than Rs.5000.
  1. Develop the program, which accepts the gross pay and produces the amount of tax owed. For a gross pay of 100000 or less, the tax is 0%; for over 100000 and 300000 or less, the tax rate is 15%; and for any pay over 300000, the tax rate is 28%. Calculate tax pay and return the result to the main method. Print the result in the main method.
  1. Write a program to check whether the input alphabet is a vowel or not. Use the switch case.
  1. Find the Student Rank based on marks.

a. From 80 – 100, Ranks A

b. From 65 – 80, Rank B

c. From 45 – 65, Rank C

d. Below 45, Rank D

  1. Write a program to check whether the input number is a prime number or not.

8. Write a program to sort exactly three numbers using only if-then-else statements

9. Write a program to reverse the digits of an integer number and write the number and its reverse to the screen.

10. Find the minimum of 3 number using the conditional operator