2. 2
Switch Case
Flow Chart of Switch Case
Creating Menus
Switch – syntax
Switch Example
Switch Default
To Switch or not To Switch
Uses of Switch Case
Summary
0utlInes
3. Switch Case
3
The last statement of each case in the
switch should almost always be a break.
The break causes program control to
jump to the closing brace of the switch
structure.
Without the break, the code flows into
the next case. This is almost never
what you want.
A switch statement will compile without
a default case, but always consider
using one.
5. 5
Creating Menus
When you want to give your user a
choice on what to do next, you can
display a set of choices (a menu).
The user then enters his or her
choice. You must validate the
choice to make sure it is valid
before you continue the program!
6. Switch - Syntax
6
The general syntax of a switch case is:
switch
and
case
are
reserved
words
switch ( expression ){
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
If expression
matches value3,
control jumps
to here
7. Switch Example
7
Example of the switch case:
switch
(option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
8. Switch Example
8
switch ( day )
{
case 0: printf (“Sundayn”) ;
break ;
case 1: printf (“Mondayn”) ;
break ;
case 2: printf (“Tuesdayn”) ;
break ;
case 3: printf (“Wednesdayn”) ;
break ;
case 4: printf (“Thursdayn”) ;
break ;
case 5: printf (“Fridayn”) ;
break ;
case 6: printf (“Saturdayn”) ;
break ;
default: printf (“Error -- invalid day.n”) ;
break ;
}
Is this structure
more efficient than
the equivalent
nested if-else
structure?
9. Switch - Default
9
A switch statement can have an optional
default case
The default case has no associated value
and simply uses the reserved word default
If the default case is present, control will
transfer to it if no other case value
matches
If there is no default case, and no other
value matches, control falls through to the
statement after the switch
10. 10
Switch With Default Case Example
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
default:
otherCount++;
break;
}
11. 11
To Switch or not To Switch
The expression of a switch statement must
result in an integral type, meaning an integer
(byte, short, int, long) or a char
It cannot be a boolean value or a floating point
value (float or double)
The implicit boolean condition in a switch
statement is equality
You cannot perform relational checks with a
switch statement
12. 12
Why Use a switch case?
A nested if-else structure is just as
efficient as a switch case.
However, a switch case may be
easier to read.
Also, it is easier to add new cases
to a switch case than to a nested if-
else structure.
13. 13
Summary
The break statement can be used as the last
statement in each case's statement list
A break statement causes control to transfer to
the end of the switch statement
If a break statement is not used, the flow of control
will continue into the next case