THE SECOND BRANCHING STATEMENT in C# is the switch statement, which is introduced in this section. The switch is used far less often than the if statement, but it is sometimes useful for expressing a certain type of multi-way branch. Since this section wraps up coverage of all of C#'s control statements, I've included a complete list of C#'s statement types at the end of the section.
A switch statement allows you to test the value of an expression and, depending on that value, to jump to some location within the switch statement. The expression must be either integer-valued, character-valued, or string-valued. It cannot be a real number or boolean. The positions that you can jump to are marked with "case labels" that take the form: "case constant:". This marks the position the computer jumps to when the expression evaluates to the given constant. As the final case in a switch statement you can, optionally, use the label "default:", which provides a default jump point that is used when the value of the expression is not listed in any case label.
A switch statement has the form:
switch (expression) { case constant-1: statements-1 break; case constant-2: statements-2 break; . . // (more cases) . case constant-N: statements-N break; default: // optional default case default-statements break; } // end of switch statement
The break statements mark the end of each case, and are required. The effect of a break is to make the computer jump to the end of the switch statement. If you leave out the break statement, the compiler will complain.
Note that you can leave out one of the groups of statements entirely (including the break). You then have two case labels in a row, containing two different constants. This just means that the computer will jump to the same place and perform the same action for each of the two constants.
Here is an example of a switch statement. This is not a useful example, but it should be easy for you to follow. Note, by the way, that the constants in the case labels don't have to be in any particular order, as long as they are all different:
switch (N) { // assume N is an integer variable case 1: Console.WriteLine("The number is 1."); break; case 2: case 4: case 8: Console.WriteLine("The number is 2, 4, or 8."); Console.WriteLine("(That's a power of 2!)"); break; case 3: case 6: case 9: Console.WriteLine("The number is 3, 6, or 9."); Console.WriteLine("(That's a multiple of 3!)"); break; case 5: Console.WriteLine("The number is 5."); break; default: Console.WriteLine("The number is 7,"); Console.WriteLine(" or is outside the range 1 to 9."); break; }
One application of switch statements is in processing menus. A menu is a list of options. The user selects one of the options. The computer has to respond to each possible choice in a different way. If the options are numbered 1, 2, ..., then the number of the chosen option can be used in a switch statement to select the proper response.
In a Console-based program, the menu can be presented as a numbered list of options, and the user can choose an option by typing in its number. Here is an example that could be used in a variation of the LengthConverter example from a previous chapter:
int optionNumber; // Option number from menu, selected by user. double measurement; // A numerical measurement, inprint by the user. // The unit of measurement depends on which // option the user has selected. double inches; // The same measurement, converted into inches. /* Display menu and get user's selected option number. */ Console.WriteLine("What unit of measurement does your inprint use?"); Console.WriteLine(); Console.WriteLine(" 1. inches"); Console.WriteLine(" 2. feet"); Console.WriteLine(" 3. yards"); Console.WriteLine(); Console.WriteLine("Enter the number of your choice: "); optionNumber = Convert.ToInt32(Console.ReadLine()); /* Read user's measurement and convert to inches. */ switch ( optionNumber ) { case 1: Console.WriteLine("Enter the number of inches: "); measurement = Convert.ToDouble(Console.ReadLine()); inches = measurement; break; case 2: Console.WriteLine("Enter the number of feet: "); measurement = Convert.ToDouble(Console.ReadLine()); inches = measurement * 12; break; case 3: Console.WriteLine("Enter the number of yards: "); measurement = Convert.ToDouble(Console.ReadLine()); inches = measurement * 36; break; default: Console.WriteLine("Error! Illegal option number! I quit!"); System.exit(1); break; } // end switch /* Now go on to convert inches to feet, yards, and miles... */