Making Decisions in C: The switch Statement

The type of if-else statement chain that you encountered in the last program exam- ple—in which the value of a variable is successively compared against different values—is so commonly used when developing programs that a special program statement exists in the C language for performing precisely this function. The name of the statement is the switch statement, and its general format is

switch ( expression )

{

case value1:

program statement

program statement

break;

case value2:

program statement

program statement

break;

case valuen:

program statement

program statement

break;

default:

program statement

program statement

break;

}

The expression enclosed within parentheses is successively compared against the values value1, value2, …, valuen, which must be simple constants or constant expressions. If a case is found whose value is equal to the value of expression, the program statements that follow the case are executed. Note that when more than one such program state- ment is included, they do not have to be enclosed within braces.

The break statement signals the end of a particular case and causes execution of the switch statement to be terminated. Remember to include the break statement at the end of every case. Forgetting to do so for a particular case causes program execution to continue into the next case whenever that case gets executed.

The special optional case called default is executed if the value of expression does not match any of the case values. This is conceptually equivalent to the “fall through” else that you used in the previous example. In fact, the general form of the switch statement can be equivalently  expressed as an if statement as follows:

if ( expression == value1 )

{

program statement

program statement

}

else if ( expression == value2 )

{

program statement

program statement

}

else if ( expression == valuen )

{

program statement

program statement

}

else

{

program statement

program statement

}

Bearing this mind, you can translate the big if statement from Program 6.8A into an equivalent switch statement,  as shown in Program 6.9.

Program 6.9   Revising  the Program to Evaluate Simple  Expressions, Version 2

/* Program to evaluate simple expressions of the form value operator   value  */

#include <stdio.h>

int main (void)

{

float value1, value2;

char  operator;

printf (“Type in your expression.\n”);

scanf (“%f %c %f”, &value1, &operator, &value2);

switch (operator)

{

case ‘+’:

printf (“%.2f\n”, value1 + value2);

break;

case ‘-‘:

printf (“%.2f\n”, value1 – value2);

break;

case ‘*’:

printf (“%.2f\n”, value1 * value2);

break;

case ‘/’:

if ( value2 == 0 )

printf (“Division by zero.\n”);

else

printf (“%.2f\n”, value1 / value2);

break;

default:

printf (“Unknown operator.\n”);

break;

}

return 0;

}

Program 6.9   Output

Type in your expression.

178.99 – 326.8

-147.81

After the expression has been read in, the value of operator is successively compared against the values as specified  by each case. When a match is found, the statements con- tained inside the case are executed. The break statement then sends execution out of the switch statement, where execution of the program is complete. If none of the cases match the value of operator, the default case, which displays Unknown operator. is executed.

The break statement in the default case is actually unnecessary in the preceding program because no statements follow this case inside the switch. Nevertheless, it is a good programming habit to remember to include the break at the end of every case.

When writing a switch statement, bear in mind that no two case values can be the same. However, you can associate more than one case value with a particular set of program statements. This is done simply by listing the multiple case values (with the keyword case before the value and the colon after the value in each case) before the common statements that are to be executed. As an example, in the following switch statement, the printf statement, which multiples value1 by value2, is executed if operator is equal to an asterisk or to the lowercase letter x.

switch (operator)

{

case ‘*’:

case ‘x’:

printf (“%.2f\n”, value1 * value2);
break;
 

}

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *