Switch
is a multiple-branch selection statement used to successively test the value of an expression against a list of character or string values. When a match is found, the statements associated with the matched case are executed. The general form of the switch statement is:-
switch (expression) {
case somevalue:
statement sequence;
break;
case somevalue2:
statement sequence;
break;
case somevalue3:
statement sequence;
break;
...
...
default:
statement sequence;
}
The value of expression is compared in order, against each of the values (somevalue, somevalue2 etc.) specified in the case
statements. The first match wins and any statements associated with the case
are executed until a break
statement is reached. If a match is not found then the statements associated with the default
case are executed.
The default
case is optional and if not present then no action takes place if all matches fail.
The break
statement is a jump statement. When a break
is encountered, program execution jumps to the nearest line of code following the switch block, i.e. after the end switch }
bracket.
Here is a simple example to illustrate:-
switch ( Grade ) {
case 'A':
cout << "Excellent\n";
break;
case 'B':
cout << "Good\n";
break;
case 'C ':
cout << "Tolerable\n";
break;
case 'D':
cout << "Poor\n";
break;
default:
cout << "You grade is a mystery\n";
}
If Grade
was given a value of 'C'
then the output would be 'Tolerable'
.
Here are a few things to note about the switch
statement.
-
Unlike the multiple decision statement that can be created using if-else
, the switch
statement evaluates the conditional expression
and tests it against numerous values.
-
Unlike if-else
, switch
can only test for equality, it cannot evaluate a relational or logical expression (i.e. Grade < 'C'
)
-
The value of the expression in a switch
statement must be an ordinal type i.e. integer
, char
, short
,long
, etc. Float
and doubles
are not allowed.
Now let's consider the importance of the break
statement.
Consider what happens if I leave the break
statement out of the example code from above:-
switch ( Grade ) {
case 'A':
cout << "Excellent\n";
case 'B':
cout << "Good\n";
case 'C ':
cout << "Tolerable\n";
case 'D':
cout << "Poor\n";
default:
cout << "You grade is a mystery\n";
}
The output if Grade
were set to 'C'
would be
Tolerable
Poor
Your grade is a mystery
This is because, when a match is found, the statements associated with the matched case are executed as normal. However, without a break
statement to jump to the end of the switch
block, execution continues into the following case's
statements until either a break
is found or the end of the switch
is reached.
Sometimes this can be useful. Consider the following:-
cout << "Please enter A or D\n";
cin >> userInput;
switch ( userInput ) {
case 'a':
case 'A':
cout << "'You chose the A option\n";
break;
case 'd':
case 'D':
cout << "'You chose the D option\n";
break;
default:
cout << "Invalid input\n";
}
This piece of code allows a user to type in a single letter - where we don't mind if the input is in upper or lowercase. Not the use of empty conditions for case 'a'
and 'd'
and no break
statement. This allows the case 'A'
statement to execute if the user types in an 'A'
or an 'a'
and the case 'D'
statement to execute if the user types in a 'D'
or an 'd'
.
Another thing to note about the switch
statement is that the statements associated with each case
are not code blocks but statement sequences. This means you cannot declare a local variable in a statement sequence.
I.e. The following is incorrect:-
switch (ch ) {
case 1:
int i;
...
The entire switch
statement however is a code block, So you could add a local variable as follows:-
switch (ch ) {
int i;
case 1:
...
If for some reason you were really determined to create a local variable in a case statement sequence, you could cheat:-
switch (ch ) {
case 1:
if (1) {
// this condition is always true
int i;
more statements;
}
...
The last example illustrates the inclusion of an if
blocks inside a case statement. You are allowed to include other types of blocks such as if-else
blocks, for
blocks and of course you can nest switch
blocks. Here is an example of nested switch
statements.
cout << "Please enter 1 for a pizza or 2 for a drink\n";
cin >> menu;
switch (menu) {
case 1:
//user selected pizza
cout << "Press 1 for ham topping, 2 for cheese topping\n";
cin >> submenu;
switch (submenu) {
case 1:
cout >> "pizza with ham topping coming right up\n";
break;
case 2:
cout >> "pizza with cheese topping coming right up\n";
break;
}
break;
case 2:
//user selected a drink
cout << "Press 1 for coke, 2 for orange juice\n";
cin >> submenu;
switch (submenu) {
case 1:
cout >> "one coke coming right up\n";
break;
case 2:
cout >> "one orange juice coming right up\n";
break;
}
break;
default:
//user didn't make a proper selection
"eel pie and mash coming up\n";
}
~Now try the activity~
Activity 8E |
- Write a program that allows a user to order a meal which may include one or all of the following, a starter, a main course and a dessert.
Using the switch statement, work out and display the bill to the user.
You can use these constanst:-
#define STARTER 2.95
#define MAIN_COURSE 6.95
#define DESSERT 1.95
|