A switch statement is a branch statement with many options for a single value to execute according to the given condition. The switch statement works like an if statement but in an efficient way.

Difference between if-else and switch statement

We prefer to switch statements on the if-else statement because if-else is used only for small operations. If we use that statement for the larger number, it will require more time, and the code will also become complex. So in alternative to that statement, the switch statement is used.

Syntax

switch (val) {

case value1:


     break;

case value2:


     break;

default:    // default statement sequence

}

We can add the number of cases according to the condition. A break statement is used to terminate the program after the condition has been met in the specified case.

Example 1: switch statement

As discussed earlier, the switch statement provides several options for the execution of source code; similarly, we have provided an integer number, and the switch statement will shift to the case according to the given number. Firstly, a system library is used to implement the code; it includes all the system features required for the C sharp programming language to execute.

Using system;

After providing a library, we create a public class in the program, as C sharp follows the approach of object-oriented programming. Then inside the main program, an integer variable is assigned by a number. We have used three cases; the given number will get switched according to the number. As we have used 7 numbers, the control first goes to case 1; as it is not true, it will continue and switch to the second case; it gets matched so that the statement will be displayed, and the break statement will be applied. We have displayed a message in the default portion in case the number does not lie in the given cases. The ‘default’ part of the switch statement acts as the ‘else’ part of the ‘if-else statement.

Console.writeLine(“case 7”);

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image4-26.png" data-lazy- height="451" src="data:image/svg xml,” width=”446″>

Save the code of the text editor with the ‘.cs’ extension. To implement the c sharp code in the Linux operating system, we need first to install mono, an open-source implementation of the .NET framework. A compiler is always required for the compilation, so MCS plays the role, and later mono executes the file by having the ‘.exe’ extension.

$ MCS file.cs

$mono file.exe

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image6-26.png" data-lazy- height="58" src="data:image/svg xml,” width=”401″>

The execution will display the case 7 messages.

Example 2: Check the vowel

For the implementation of the switch statement, there could be many possibilities as there exist many operational examples. One of the simple ways is to check a specified alphabet if it is a vowel or not. Inside the main program, a character variable is declared that will store the value entered by the user. A message will be displayed at the console when the program is executed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image5-26.png" data-lazy- height="193" src="data:image/svg xml,” width=”578″>

The entered word is converted into character; if it is not, this function contains the readline() function as a parameter to get the value.

Ch = convert.ToChar (console.ReadLine());

After that, we used the switch statement. The statement will take the character that is first converted into the lower case alphabet if it is in the upper case alphabet because all the vowels are declared in the lower case.

Switch(char.ToLower(ch))

To.lower() function is a built-in function that takes the character argument and returns the value in the lower case version of that character.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image8-17.png" data-lazy- height="404" src="data:image/svg xml,” width=”495″>

According to all vowels, 5 cases will be declared here to check for each vowel alphabet. For example, the entered word is “a,” then when the switch statement passes the character, case “a” will be executed, and the break statement will terminate the program. Similarly, all other 4 vowels will follow the same approach. The default statement will be displayed in case the entered word is not a vowel.

Now execute the program to see the working of the switch statement regarding vowels. When the program runs, the user will get a message to enter the value. He will enter an alphabet; the system will check if it is in upper case, then first, it will be converted. In the example, the user has entered ‘h,’ the system will go case by case in the switch statement, and it is not a vowel so the default part will get executed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image7-22.png" data-lazy- height="96" src="data:image/svg xml,” width=”386″>

Example 3: switch statement with grouped cases

Consider the previous example. In the switch statement, all the vowels are written collectively following different cases. In the previous declaration, each case follows the break statement separately, but here we have written a single output statement and the break statement. This causes a reduction in space, and the code is mode easy to understand; moreover, the compiler needs less time to compile and execute the code efficiently. This approach is feasible and applied in situations where more than one case has to do some work or display the same output. For instance, this example contains the same output values, so we have applied it in the code. Sample syntax is described below.

Case ‘a’:

Case ‘b’:

Case ‘c’:

Console.writeline(“output”);

Break;

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image2-28.png" data-lazy- height="296" src="data:image/svg xml,” width=”487″>

Example 4: switch statement with the goto statement

A ‘goto’ statement is used when we need to jump to any other point in the source code; this is also known as a jump statement because it makes the compiler jump to the specified point. This specified point is known as a labeled location in C sharp. The label is the case name here. Mostly it is the default statement. In general, we use a break statement to stop the program, but we need to execute the default statement; hence, the goto statement will be used.

The lower example will contain an integer number. This number will get switched and move towards any case we have declared. Each case will display greetings depending on the case we select. The first case will display the message, and then the goto statement will jump towards default, so if a person selects 1 case, both 1 and default values will be displayed. Moving towards case 2, a message will be displayed, and then the goto statement will jump to case 3. So with case 2, case 3 will be displayed too. Whereas case 3 contains a jump statement for default too.

Case 2:Console.writeline(“hi”);

Goto case 3;

Here case 3 is a labeled location.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-30.png" data-lazy- height="386" src="data:image/svg xml,” width=”588″>

Now execute the code, as we have selected 2, so the second and the 3rd greetings are displayed with the default value because the third statement has the goto of the default value.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image3-27.png" data-lazy- height="94" src="data:image/svg xml,” width=”426″>

Conclusion

The switch statement in C sharp is implemented the same way as we use it in other programming languages. We provide the value passed through each case to meet the requirement. The switch statement is preferred over the if-else statement as it provides more options and is used for the absolute results. We implemented c sharp examples in Ubuntu 20.04 using mono and MCS compilers for the execution.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Author-Image-150×150.jpg6258ebdba7fc3.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.