3 examples to learn C# switch case statement

The C# switch  statement

The switch statement is a decision-making statement which is used to execute a block of code when you have various blocks in C# program. The C# switch is useful when you have long blocks of if, if..else statement, otherwise.

You may choose using the switch statement when you have a single variable to check for many possibilities.

The case statement

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

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

Syntax of switch case statement

Following is the general structure of the switch case statement:

 

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

An example of using the switch-case statement

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

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

C# switch

See online demo and code

First, I declared an int variable, daynumber, which is assigned the 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:

 

C# break statement

As you can see, I used the break statement in the above example with each case statement. The break is an optional statement in switch case. However, if you do not use the 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 I 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 execute.

See online demo and code

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

An example of switch C# statement with string

Following is an example of  switch 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:

C# switch case string

See online demo and code

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