C# switch case statement with examples

The switch statement

The switch statement is a decision-making statement which is used to execute a block of code when you have various blocks. The C# switch is useful when you have long blocks of if, if..else statement. You may choose to use the switch statement when you have a single variable to check for many possibilities.

See an example of switch case

C# Case

The switch statement is used with C# case statement. In the switch case statement a variable/expression is tested against a list of values, each value is taken as a case.

Before looking at examples of using C# switch case, let us first look at its structure.

Syntax of switch case C#

Following is the general structure of switch case statement:

switch (expression) {

 

   case value :

 

    // Code to be executed if condition is true

 

    break; //This is optional, The execution will stop here for switch statement

 

   case value :

 

    // Code to be executed if condition is true

 

    break; //This is optional, The execution will stop here for switch statement

 

   case value :

 

    // Code to be executed if condition is true

 

    break; //This is optional, The execution will stop here for switch statement

 

default:  // This is optional and works like else in if statement

 

    // Code to be executed if condition is true

 

}

We will explain C# break and default statements at the bottom part of this tutorial. Now let us look at a few examples of using switch case.

A switch-case example

Following is a switch statement example. As you run this example by pressing F5 in the editor, you will be asked to enter a day number from 1-7. Where 1 is for Mon and 7 is for Sunday.

We have seven cases in the program, one for each day. As you enter a day number and press the enter key, the code will display day name of the number that you entered. See example by clicking the link below:

Experience this example online


First we declared an int variable daynumber which is assigned entered value from the keyboard.  Then seven cases are created, one for each day. Once a case is evaluated as true, it will display the day name and execution will be out of switch statement to this statement: Console.ReadLine();.

C# switch break

As you can see, we used the break statement in the above example with each case statement. C# break is an optional statement in switch case. However, if you do not use break statement the following cases will keep on evaluating.

The switch default statement

The C# default statement is used if none of the case evaluates as true. In the situation where no case is true the block of code inside the default block will be executed. It is just like the else statement in if..else.  See the following example of using the default in C#.

Switch case with default statement example

This is a replica of above example except we added a default block. As you run the program, you will be asked to enter a day number. If you enter a value other than 1-7 the default block will be executed.

Experience this example online


You can see, if you enter a number other than 1-7, it will the execute default block.

Switch string example

Following is an example of C# switch and case with a string variable. We have created a variable USState and assigned it a value. Three cases with three States and a default case is created. See code and example output by clicking the link below:

Experience this example online


You can see, the State Name is:  Illinois is displayed by using the switch C# statement.

Also see C# if

Was this article helpful?

Related Articles

Leave A Comment?