You are on page 1of 19

CSI 124

Structured Programming Language

Lecture 04
Switch
Conditional Statements
Control or decision making statement

• if statement
• switch statement
SWITCH
Multiple Selection:
The switch Statement
multiway
expression

value1 value2 value3 value4


action 1 action 2 action 3 action 4
Multiple Selection:
The switch Statement
Syntax:
switch (<selector expression>) {
case <label1> : <sequence of statements>;
break;
case <label2> : <sequence of statements>;
break;
case <labeln> : <sequence of statements>;
break;
default : <sequence of statements>;
}
The switch Multiple-Selection Structure
switch ( integer expression )
{
case constant1 :
statement(s)
break ;
case constant2 :
statement(s)
break ;

...
default :
statement(s)
break ;
}
switch Statement: Example 1
• If you have a 95, what grade will you get?
#include<stdio.h>
void main(){
int score=100;
switch(score/10){
case 10:
case 9: printf("A\n");
case 8: printf("B\n");
case 7: printf("C\n");
case 6: printf("D\n");
default:printf("F\n");
}
}
switch Statement: Example 2
• If you have a 95, what grade will you get?
#include<stdio.h>
void main(){
int score=100;
switch(score/10){
case 10:
case 9: printf("A\n");break;
case 8: printf("B\n");break;
case 7: printf("C\n");break;
case 6: printf("D\n");break;
default:printf("F\n");
}
}
switch Statement: Example 2
is equivalent to:
if (score >= 90)
printf("Grade = A“);
else if (score >= 80)
printf("Grade = B“);
else if (score >= 70)
printf("Grade = C“);
else if (score >= 60)
printf("Grade = D“);
else // score < 59
printf("Grade = E“);
Nested Switch Statement
• switch(ch1) {
case 'A': printf("This A is part of outer switch" );
switch(ch2) {
case 'A': printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}
break;
case 'B': /* case code */
}
Points to Remember
• The expression followed by each case label
must be a constant expression.
• No two case labels may have the same
value.
• Two case labels may be associated with the
same statements.
• The selector expression can only be: a bool,
an integer, an enum constant, or a char.
Points to Remember
• In case label we can not use any identifier.
ex: case a+2
• The default label is not mandatory.
• There can be only one default label, and it
is usually last.
• The break causes program control to jump to
the closing brace of the switch structure.
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!
Good Programming Practices
• Include a default case to catch invalid data.
• Inform the user of the type of error that has
occurred (e.g., “Error - invalid day.”).
• If appropriate, display the invalid value.
switch Example
switch ( day )
{
case 0: printf (“Sunday\n”) ;
break ;
case 1: printf (“Monday\n”) ;
break ;
Is this structure more
case 2: printf (“Tuesday\n”) ; efficient than the
break ;
case 3: printf (“Wednesday\n”) ; equivalent nested if-else
break ;
case 4: printf (“Thursday\n”) ;
structure?
break ;
case 5: printf (“Friday\n”) ;
break ;
case 6: printf (“Saturday\n”) ;
break ;
default: printf (“Error -- invalid day.\n”) ;
break ;
}
Why Use a switch Statement?
• A nested if-else structure is just as efficient as
a switch statement.
• However, a switch statement may be easier to
read.
• Also, it is easier to add new cases to a switch
statement than to a nested if-else structure.
References
• www.cse.ust.hk/~liao/comp102/PPT/switch.p
pt
Thanks for your time and attention.

You might also like