maandag 18 juni 2007

The switch Statement

Suppose you create a screen menu that asks the user to select one of five choices, you can extend an if else if else sequence to handle five alternatives, but the C++ switch statement more easily handles selecting a choice from an extended list. Here's the general form for a switch statement:

switch (integer expression)
{
case label1: statement(s)
case label2: statement(s)
...
default : statement(s)
}

A C++ switch statement acts like a routing device that tells the computer which line of code to execute next.
On reaching a switch statement, a program jumps to the line labeled with the value corresponding with the value of integer-expression.
For example, if integer-expression has the value 4, the program goes to the line that has case 4:label.
The value integer-expression must be an expression that reduces into an integer value.
Also each label must be an integer constant expression!
Most often labels are simple int or char constants, such as 1 or 'q', or enumerators. If integer-expression doesn't match any of the labels, the program jumps to the line labeled default. The default label is optional. If you omit it and there is no match, the program jumps to the next statement following the switch.

Execution of a label, does not automaticaly stop at the next label!
So use a break at the end of each label to stop execution.

Example:

switch (num)
{
case 1 : statement1
break;
case 2: statement2
break;
case 3: statement3
break;
default statement4
}

The reason why switch works with the break's can be shown here in this example:

char choice;
cin >> choice;
while (choice != 'Q' && choice != 'q')
{
switch(choice)
{
case 'a':
case 'A': cout << "\a\n";
break;
case 'r':
case 'R': report();
break;
case 'l' :
calse 'L': cout << "The boss was in all day.\n";
break;
case 'c' :
case 'C': comfort();
break;
default : cout << "That's not a choice.\n";
}
showmenu();
cin >> choice;
}

Because there is no break immediately following case 'a', program execution passes on to the next line, which is the statement following case 'A'.

You can also use enumerations as labels:

switch (code)
{
case red : cout << "Here lips were red.\n"; break;
case orange: cout << "Her hair was orange.\n"; break;
}

Where code is the value of one of the enumerators.
enum was used to fedine a set of related constants and then using the constants in a switch statement. In general, cin doesn't recognize enumerated types (it can't know how you will define them), so the program reads choice as an int. When the switch statement compares the int value to an enumerator case label, it promotes the enumerator to int.

SWITCH and IF ELSE
Both the switch statement and the if else statement let a program select from a list of alternatives. The if else is the MORE VERSITILE of the two.
For example it can handle ranges of values, where switch needs constants as values to make decisions.
The value must be integer when using switch, while the if else statement can handle floating point numbers / variables.
So if you need ranges to test and based on that make decisions, or you need to make decisions based on floating point tests, you need to use if else statement(s).
Otherwise if you need to make choices based on constant values of type int, switch is probably designed to handle that more efficiently.

Tip: If you can either use an if else if sequence or a switch statement, the usual rule is to use a switch if you have three or more alternatives!

Geen opmerkingen: