YouTip LogoYouTip

Csharp Switch

[![Image 1: C# Decision Making](#) C# Decision Making](#) A **switch** statement allows testing a variable against multiple values. Each value is called a case, and the variable being tested is checked against each **switch case**. The syntax for the **switch** statement in C# is: switch(expression){ case constant-expression : statement(s); break; case constant-expression : statement(s); break; /* You can have any number of case statements */ default : /* Optional */ statement(s); break; } The **switch** statement must follow these rules: * The **expression** in a **switch** statement must be of an integer or enum type, or a class type that has a single conversion function to convert it to an integer or enum type. * You can have any number of case statements within a switch. Each case is followed by the value to be compared and a colon. * The **constant-expression** for a case must be the same data type as the variable in the switch, and it must be a constant. * When the variable being tested matches a case's constant, the statements following that case are executed until a **break** statement is encountered. * When a **break** statement is encountered, the switch terminates, and the flow of control jumps to the next line following the switch statement. * Not every case needs to contain a **break**. If a case statement is empty, it can omit the **break**, and the flow will _fall through_ to the next case until a break is encountered. * C# does not allow fall-through from one case section to the next. If a case statement has been executed, it must contain a **break** or another jump statement. * A **switch** statement can have an optional **default** case at the end. The default case is used to perform a task when none of the cases are true. It is good practice to include a **break** statement in the default case as well. * C# does not support explicit fall-through from one case label to another. To enable explicit fall-through from one case label to another in C#, you can use `goto` a switch-case or `goto default`. ![Image 2: switch Statement in C#](#) The following example is used to determine the current day of the week: ## Example using System; namespace MyApplication { class Program { static void Main(string[] args) { int day =4; switch(day) { case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break; case 4: Console.WriteLine("Thursday"); break; case 5: Console.WriteLine("Friday"); break; case 6: Console.WriteLine("Saturday"); break; case 7: Console.WriteLine("Sunday"); break; } } } } The execution result varies depending on the current date. The result for the day I executed this is: Thursday The following example evaluates a student's grade and includes a **default** statement: ## Example using System; namespace DecisionMaking { class Program { static void Main(string[] args) { /* Local variable definition */ char grade ='B'; switch(grade) { case'A': Console.WriteLine("Excellent!"); break; case'B': case'C': Console.WriteLine("Well done"); break; case'D': Console.WriteLine("You passed"); break; case'F': Console.WriteLine("Better try again"); break; default: Console.WriteLine("Invalid grade"); break; } Console.WriteLine("Your grade is {0}", grade); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: Well doneYour grade is B [![Image 3: C# Decision Making](#) C# Decision Making](#)
← Csharp Nested SwitchCsharp Nested If β†’