Switch Statement
Before reading this tutorial, you should have knowledge of if else statements.
The switch statement is a type of control structure. The general form of a switch statement is
switch (expression)
case constant 1 :
statement sequence
break;
case constant 2 :
statement sequence
break;
.
.
default:
statement sequence
The switch statement allows doing multiple selections depending upon the value of expression. It tests the value of the expression against the integer or character constants. The value of the expression must be integer or character. The value of the expression is tested against the constants in the case statements. When a correct match is found sequence of statements associated with the constant is executed until the break statement or the end of the switch statement is reached. If no match is found then sequence of statements associated with default is executed. The case values appear in case labels. No two or more case labels can contain same constant values. The case values can occur in any sequence. Here is a program which illustrates the working of switch statement.
The result of the program is: -
The user has entered the number 3 corresponding to the statement
cin >> c;
The value of variable c is now 3. The switch statement
switch(c)
is now executed. The value of the variable c is matched against the constants in the case statements. The value matches with the constant 3 in the case label and as a result the statement
cout << “ You have chosen cookies” << endl;
is executed. The statement
break ;
switches the control to the statement following the switch which is the statement
return(0);
If the user has entered value other than the constants in the case labels then the statement associated with the default would have been executed. Here is the result of the same program which shows the execution of default statement.
Now the user has entered the value 7. The value of variable c is 7. The value 7 does not
match with any of the constants in the case labels and as a result statement
cout << “ You have not chosen any item” << endl;
is executed.
The break statement is necessary as it switches the control to the statement following the switch statement after executing sequence of statements associated with case value. If the break statement is omitted then the sequence of statements for all the following cases are executed till a break statement is encountered or end of the switch is reached. Here is a program which shows how the statements are executed when there is no break statement.
The result of the program is: -
The user has entered value 1. The value of the variable c is 1. The switch statement is executed. The value of the variable c is matched against the constants in the case labels. It matches with the value of the first case statement. As there is no break statement the execution is transferred to the statement
case (2) :
As there is no break statement associated with this case statement also the control is transferred to the third case statement
case (3) :
The next statement
cout << “ You have chosen one of the first 3 items” << endl;
is executed. Then the statement
break;
is executed and control is transferred to the statement following the switch statement. Omitting the break statement can be helpful when sequence of statements for two or more case statements is same. For example, in this program statement
cout << You have chosen one of the first 3 items “ <<
is same for first 3 case statements. Instead of typing the same statement along with every case label you can ignore the break statement with the case statements and insert the break statement where termination is necessary. It avoids unnecessary duplication of sequence of statements.
The statement switch can be used instead of if statement. Instead of using ifs statements for checking the expression against two or more constant values, switch should be used. Here is a program which performs the same function but uses ifs statements.
The task of writing nested ifs is tedious and switch should be used.
After an introduction of switch statements, let us move on to discuss loop statements.
Go to the previous lesson or proceed to the next lesson