- if Statements
- switch Statements
- C# Loops
- goto Statements
- break Statements
- continue Statements
- return Statements
- Summary
switch Statements
When there are many conditions to evaluate, the if-else if-else statement can become complex and verbose. A much cleaner solution for some situations is the switch statement. The switch statement allows testing any integral value or string against multiple values. When the test produces a match, all statements associated with that match are executed. Here's the basic form of a switch statement:
switch(integral or string expression) { case <literal-1>: statement(s) break; . . . case <literal-n>: statement(s) break; [default: statement(s)] }
For C++ Programmers
C++ accepts only integer values in a switch statement. C# accepts strings and enums as well as integers.
Also, C++ permits case fall-through. C# does not. break statements are mandatory in C# unless two cases are combined with no statements between them.
For Java Programmers
Java accepts integer values in a switch statement. C# accepts strings as well as integers.
Also, Java permits case fall-through. C# does not. break statements are mandatory in C# unless two cases are combined with no statements between them.
The integral, enum, or string expression is compared against each case statement's literal value. Add as many case statements as necessary. When there's a match, those statements following the matching case are executed. Here's an example:
switch (choice) { case "A": Console.WriteLine("Add Site"); break; case "S": Console.WriteLine("Sort List"); break; case "R": Console.WriteLine("Show Report"); break; case "Q": Console.WriteLine("GoodBye"); break; default: Console.WriteLine("Huh??"); break; }
The break statement is mandatory, even on the default case. One case can't drop through to another case after executing its statements. There are a few less common exceptions to this rule. One exception is grouping case statements together, as this example shows:
switch (choice) { case "a": case "A": Console.WriteLine("Add Site"); break; case "s": case "S": Console.WriteLine("Sort List"); break; case "r": case "R": Console.WriteLine("Show Report"); break; case "q": case "Q": Console.WriteLine("GoodBye"); break; default: Console.WriteLine("Huh??"); break; }
The example above shows an exception to the restriction against case fall-through. The case for each capital and small letter are grouped together with one immediately following the other. The top case will fall through to the next case when there are no statements between the two cases. Other ways to conclude a case include the throws clause, return statement, and goto statement. Here's an example of using a goto statement:
switch (choice) { case "A": Console.WriteLine("Add Site"); break; case "S": Console.WriteLine("Sort List"); break; case "R": Console.WriteLine("Show Report"); break; case "V": Console.WriteLine("View Sorted Report"); // Sort First goto case "R"; case "Q": Console.WriteLine("GoodBye"); break; default: Console.WriteLine("Huh??"); break; }
The example above shows the second exception to the restriction against case fall-through. It uses a goto statement to execute another case. It doesn't matter whether the goto case is the next in line or somewhere else in the switch statement. Program control will still transfer to the case specified in the goto statement. When none of the cases matches, control transfers to the default case.
WARNING
Although the default case in a switch statement is optional, it should normally be included. Its absence has been known to create subtle bugs that occur when none of the cases match.
The default case in a switch statement is optional. When there is no default case, program control transfers to the next statement following the ending curly brace of the switch statement. The following example shows a switch statement without a default case.
switch (calculation) { case 1: // perform calculation #1 break; case 2: // Perform calculation #2 break; case 3: // Perform calculation #3 break; }
In the preceding code, three calculations can be performed as a result of a deliberate choice. If no choice were made, the user would expect some default calculation to take place. Clearly, this would not happen. The result is a bug whose consequences depend upon the severity of not performing the default action.
TIP
Switch statements are very efficient with conditions requiring equality relations and very small ranges. For examples cases for 3 to 5 would be manageable, but a range between 1 and 500 is probably inappropriate. For anything more sophisticated, evaluation-wise, go with the if statement.