Updated July 7, 2023
Introduction to Switch Statement in C#
This article will see an outline of Switch Statement in C#; C # is a very powerful object-oriented programming language of.Net Framework. The .Net framework is an extensive, revolutionary platform using which the following applications such as Windows applications, Web applications, Web Services, etc., can be easily developed.
What is a Switch Statement in C#?
Below the points explain the switch statement in c#:
- Consider a case where you have been given a bunch of keys of different sizes. Now you are asked to open a door using one of the keys from this bunch. So what will be your approach towards this problem? It is simple, and you guessed it right, you will pick one key and try to unlock the door using it. If this key does not open the door, you try with another key.
- The process continues until you’ve finally found the key, which unlocks the door. After the key is found and the door is unlocked, you stop. In case if you are able to find the key in the first attempt you will not keep on trying with the other keys after that, correct?
- Similar is the case with the switch statement. This example can help you easily understand the basic definition and flow of the switch statement. The basic flow and functionality of the switch statement remain the same in all the programming languages. The difference can be seen only in the general syntax based on the programming language being used. In a very basic term, a switch statement evaluates an expression, tests it and compares it against the several cases written in the code.
- As soon as the match with any case is found, the control enters into this case and start executing the statements written within this case until a break statement is encountered. As soon as a break statement appears, the switch statement terminates and the program control exits switch.
- It might sometimes happen that no case matches up with the value of the expression. For such cases, we mention a default case that will always execute in case if no match is found. The cases in a block of the switch statement are represented by different numbers or string, which is known as an identifier. The value of the expression or the value provided by the user is compared with these cases until the match is found.
The syntax for switch statement in C# programming language is given below.
Syntax:
switch( expression )
{
case value1:
//Block of code;
break;
case value2:
//Block of code;
break;
case valueN:
//Block of code
break;
default:
//Block of code
break;
Flowchart of Switch Statement in C#
Below is the flowchart of the switch statement in C#:
How does Switch Statement work in C#?
Let us understand the flow of control depicted in the flowchart above in order to gain a better understanding of the flow of execution. An expression is passed with the switch statement which is equal to one of the values of the cases. In case the value is not equal, the default case is executed. The value of this expression is then compared with the case identifier or the first case. If the first case matches then the block of code associated with the first case is executed. Once the break is encountered, the execution stops and you will exit the switch statement. However, if the case does not match, the execution flows to the next case. If this case matches, then the second code block executes otherwise, the flow checks the next case in a similar way. Finally, if no case matches then the default code block is executed.
Examples of Switch Statement in C#
Lets us see some of the examples of the switch statement in C#
Example# 1
This example will give more clarity about the use of switch statements.
Code:
using System;
public class Example1 {
public static void Main(String[] args)
{
char grade_report = 'D';
Console.WriteLine( "Your performance is : " );
switch(grade_report)
{
case 'A' :
Console.WriteLine("Outstanding Result!\n" );
break;
case 'B' :
Console.WriteLine( "Excellent Result!\n" );
break;
case 'C' :
Console.WriteLine( "Good Result\n" );
break;
case 'D' :
Console.WriteLine( "Satisfying Result\n" );
break;
case 'F' :
Console.WriteLine( "Poor Result\n" );
break;
default :
Console.WriteLine( "You did not appear for exam\n" );
break;
}
}
}
Output:
Example #2
This example depicts the use of the break statement in the switch. If the break statement is not specified after the case, the execution flow will continue until it encounters the break statement.
Code:
using System;
public class Example2
{
public static void Main(String[] args)
{
int range_of_number=50;
switch (range_of_number)
{
case 10:
case 20:
case 30:
Console.WriteLine( "The number is 10 or 20 or 30 " );
break;
case 50:
case 55:Console.WriteLine( "This case also executes because there is no break " );
Console.WriteLine( "\n" );
break;
case 60:
Console.WriteLine( "The number is either 40 or 50 or 60" );
break;
default:
Console.WriteLine( "The number is greater than 60" );
break;
}
}
}
Output:
Example #3
In this example, you will see how switch statement work if the break statement is specific.
Code:
using System;
public class Example3 {
public static void Main(String[] args)
{
int x = 10, y = 5;
bool a = (x==y && x+y<10);
switch(a)
{
case true:
Console.WriteLine( "hi" );
break;
case false:
Console.WriteLine( "bye" );
break;
}
}
}
Output:
Conclusion
Switch case statements are a control statement that is regarded as a substitute for if-else statements. It is a multiway branch statement that provides a way to organize the flow of execution to parts of code based on the value of the expression.
Recommended Articles
This has been a guide to the Switch Statement in C#. Here we discuss the working of the switch statements in C#, its syntax, flow chart along with different examples and code implementation. You can also go through our other suggested articles to learn more –