The switch Statement

Summary: The switch statement can perform multiple branches.

The switch statement offers an elegant option in situations that require multiple branches. It tests a single expression that can have several values, providing a different action for each value.

One disadvantage of if and else is that they only allow one branch per keyword. The program either executes the statement that follows the if or else, or it doesn't. To perform more complex tests, you have to pile on more if and else statements, as in the ELSE1.C program above.

A program that handles keyboard input, for instance, may require several different responses to various keystrokes. The ELSE1.C program above used combinations of if and else to process keyboard input. We've used a single switch statement in the SWITCH.C program below to do the same job:

/* SWITCH.C: Demonstrate switch statement. */

#include <stdio.h>

main()

{

char ch;

printf( "Type 1 for Choice 1, type 2 for Choice 2.\n" );

ch = getchar();

switch( ch )

{

case '1':

printf( "Choice 1\n" );

break;

case '2':

printf( "Choice 2\n" );

break;

default:

printf( "Bye bye\n" );

break;

}

}

The SWITCH.C program produces the same output as ELSE1.C. Figure 3.5 illustrates the switch statement in SWITCH.C, comparing it with the if-else construct in ELSE1.C.

As in other decision-making statements, the parentheses after the keyword contain the expression to test. This can be any expression that yields a constant value. The test expression in SWITCH.C evaluates the value of the variable ch:

switch( ch )

Summary: The switch statement branches to one of several labeled alternatives.

The test expression is followed by a statement block enclosed in curly braces. The block contains alternate sections of code that you want to execute under various circumstances. The program's action branches to one of the alternatives, depending on the value of the test expression.

Each alternative in the statement block starts with a “case label,” which consists of the case keyword, a constant or constant expression, and a colon. (The only other C statement that uses labels is goto, which we'll discuss later in this chapter.)

Below is the first case label in SWITCH.C:

case '1':

This case label lists the character constant '1'. If the variable ch equals '1', the program's action branches to this label. If ch equals '2', the program branches to the following label:

case '2':

The basic effect of switch is similar to the SELECT CASE statement in QuickBasic. The program can branch to many different alternatives, but only one at a time.

A switch statement can have as many case alternatives as you need. Each alternative must be labeled with a constant value. (You can't use a variable in the label.)

NOTE:

In some previous versions of QuickC, the constant in a case label could only be a char or int value. In QuickC for Windows, the constant can be any integral type, including a long or unsigned long as well as a char or int. Chapter 4, “Data Types,” describes the char, int, and long types.

Summary: The default keyword is used only in switch statements.

SWITCH.C also shows how to use the default keyword in a switch statement. The statements after the default label execute if the value of the test expression doesn't equal any of the values listed in other labels. In SWITCH.C, the code following default executes when the variable ch equals anything other than '1' or '2'.

Not every switch statement requires a default label. If no default is present, and the test expression doesn't match any of the values listed in the other case labels, no statements are executed.

Summary: Use the break keyword to exit a switch statement.

You normally place a break statement at the end of each alternative, as shown in SWITCH.C. The break statement exits the switch statement block immediately. If you don't put a break at the end of the alternative, the action falls through to the next statement.

For instance, say that you remove all the break statements from SWITCH.C, as shown below:

switch( ch )

{

case '1':

printf( "Choice 1\n" );

case '2':

printf( "Choice 2\n" );

default:

printf( "Bye bye" );

}

If you run the revised program and type the numeral 1, the program executes the first alternative, producing this output:

Choice 1

then goes on to execute the statements that follow:

Choice 2

Bye bye

Occasionally, you may want to fall through from one case alternative to another. But you should be careful not to omit break statements accidentally. (For more information, see “Omitting break Statements from a switch Statement”.)

If you end each alternative with a break, as in SWITCH.C, the order of the alternatives isn't critical. The program branches to the label containing the correct value, no matter where that label appears in the switch statement block. For instance, you can reverse the order of the alternatives in SWITCH.C without changing the program's output. For readability's sake, many programmers put default at the end of a switch statement and arrange the other alternatives alphabetically or numerically.

Sometimes you'll want to execute the same code for more than one case. This is done by grouping all the desired labels in front of one alternative. For instance, if you revise the second alternative in SWITCH.C to read

case '2':

case '4':

case '8':

printf( "You chose 2, 4, or 8\n" );

break;

the program will print

You chose 2, 4, or 8

when you type any of those numbers and press ENTER.