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.
Below the points explain the switch statement in c#:
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;
Below is the flowchart of the switch statement 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.
Lets us see some of the examples of the switch statement in C#
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:
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:
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:
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.
The above is the detailed content of Switch Statement in C#. For more information, please follow other related articles on the PHP Chinese website!