The switch statement is very similar to the if-else-if ladder of selection statements. Here is its general form:
switch(expression)
{
case constant_1:
statement_1 or block_1;
break;
case constant_2:
statement_2 or block_2;
break;
case constant_1:
statement_3 or block_3;
break;
// More cases may follow
default
statement_X or block_X
}
In the switch construct, as in the if-else-if construct, multiple possible blocks of code are being selected from, and only one of them will typically be executed. Each case or possible block of code is initiated by the appearance of the case reserved word, and is terminated by the appearance of the break reserved word. (The break is not required, but is almost always used.) The if-else-if and the switch structures have very similar usage in a program, but there are distinct differences. These differences are presented in the table below.
if-else construct
switch construct
An expression is evaluated and the code is selected based on the truth value of the expression. An expression is evaluated and the code is selected based on the value of the expression.
Each if has its own logical expression to be evaluated as true or false. Each case is referring back to the original value of the expression in the switch statement.
The variables in the expression may evaluate to a value of any type, either an int or a char. The expression must evaluate to an int.
There is one subtle but very important difference between the if-else-if ladder and the switch statement. That difference involves the break statement. In the if-else-if ladder, no matter what, only one of the blocks of code is executed. In the switch construct, if the break statement is omitted, the flow of execution will go forward into the next block. Leaving out a break is sometimes useful, but pretty rare. If you are missing a break statement in any block, make sure you intended to leave it out!
Example
#include
int main(void)
{
float a, b;
char operation;
cout << "Enter a number, an operation, and a number:";
cin >> a >> operation >> b;
cout << endl << endl << "The answer, Master, is: ";
switch (operation)
{
case '+':
cout << a + b;
break; // Without this statement, execution
// continues on to case '-'
case '-':
cout << a - b;
break;
case 'x': // Users may use either symbol for
// multiplication.
case '*': // Notice that for either case, the next
// block will be executed.
cout << a * b;
break; // Without this statement, execution
// continues on to case '/'
case '/':
cout << a / b;
break;
}
return(0);
}
Another example, one which uses more selection structures than just switch, is the problem of finding the possible number of roots of a polynomial function will depend on the degree, or value of the highest exponent in the polynomial. In a program to report how many roots a polynomial has, we might see the following code. Recall that a polynomial of degree 1 is a linear function. The twist in this case is that more than one case value will result in the same output. In a situation like this, the different case constants can be listed immediately before the code that should be executed, without each case having a break statement.
// code fragment to describe the number of solutions
// of a polynomial function (degree < 10).
// Assume that the coefficients are a0, a1, a2, a3, and so on,
// and that the degree of the polynomial has already been calculated.
switch(degree) // Which code to execute depends on
// the degree of the poly
Answered by Shilpa Behl
at
10:00 PM on April 17, 2008