Tuesday, September 21, 2010

Selection Statements switch

C/C++ has a built-in multiple-branch selection statement, called switch, which
successively tests the value of an expression against a list of integer or character
constants. When a match is found, the statements associated with that constant are
executed. The general form of the switch statement is
switch (expression) {
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
..
default
statement sequence
}
The expression must evaluate to a character or integer value. Floating-point expressions,
for example, are not allowed. The value of expression is tested, in order, against the
values of the constants specified in the case statements. When a match is found, the
statement sequence associated with that case is executed until the break statement or
the end of the switch statement is reached. The default statement is executed if no
matches are found. The default is optional and, if it is not present, no action takes place
if all matches fail.
Standard C specifies that a switch can have at least 257 case statements. Standard
C++ recommends that at least 16,384 case statements be supported! In practice, you
will want to limit the number of case statements to a smaller amount for efficiency.
Although case is a label statement, it cannot exist by itself, outside of a switch.
The break statement is one of C/C++'s jump statements. You can use it in loops as
well as in the switch statement (see the section "Iteration Statements"). When break is
encountered in a switch, program execution "jumps" to the line of code following the
switch statement.
There are three important things to know about the switch statement:
The switch differs from the if in that switch can only test for equality,whereas
if can evaluate any type of relational or logical expression.
No two case constants in the same switch can have identical values. Of course,
a switch statement enclosed by an outer switch may have case constants that
are the same.
If character constants are used in the switch statement, they are automatically
converted to integers.
The switch statement is often used to process keyboard commands, such as menu
selection. As shown here, the function menu() displays a menu for a spelling-checker
program and calls the proper procedures:
void menu(void)
{
char ch;
printf("1. Check Spelling\n");
printf("2. Correct Spelling Errors\n");
printf("3. Display Spelling Errors\n");
printf("Strike Any Other Key to Skip\n");
printf(" Enter your choice: ");
ch = getchar(); /* read the selection from
the keyboard */
switch(ch) {
case '1':
check_spelling();
break;
case '2':
correct_errors();
break;
case '3':
display_errors();
break;
default :
printf("No option selected");
}
}
Technically, the break statements inside the switch statement are optional. They
terminate the statement sequence associated with each constant. If the break statement
is omitted, execution will continue on into the next case's statements until either a
break or the end of the switch is reached. For example, the following function uses the
"drop through" nature of the cases to simplify the code for a device-driver input
handler:
/* Process a value */
void inp_handler(int i)
{
int flag;
flag = -1;
switch(i) {
case 1: /* These cases have common */
case 2: /* statement sequences. */
case 3:
flag = 0;
break;
case 4:
flag = 1;
case 5:
error(flag);
break;
default:
process(i);
}
}
This example illustrates two aspects of switch. First, you can have case statements
that have no statement sequence associated with them. When this occurs, execution
simply drops through to the next case. In this example, the first three cases all execute
the same statements, which are
flag = 0;
break;
break statement is present. If i matches 4, flag is set to 1 and, because there is no break
statement at the end of that case, execution continues and the call to error(flag) is
executed. If i had matched 5, error(flag) would have been called with a flag value of −1
(rather than 1).
The fact that cases can run together when no break is present prevents the
unnecessary duplication of statements, resulting in more efficient code.

No comments:

Post a Comment