Lecture08 Decision Switch
Lecture08 Decision Switch
Lecture08 Decision Switch
Decision making in C
Asma Majeed
1
The switch statement
Agenda
To be followed in today’s lecture
Switch format
Switch components
• The switch statement is often faster than nested if...else (not always).
Also, the syntax of switch statement is cleaner and easy to understand.
int choice
case 3:
switch( choice ) {
printf( “ Apple ”) ;
case 0:
break;
printf( “ Mango ”) ;
case 4:
break;
printf( “ Pear ”) ;
case 1:
break;
printf( “ Banana ”) ;
default:
break;
printf( “ Invalid Choice ”) ;
case 2:
break; }
printf( “ Grapes ”) ;
break;
case ’B’:
case ’C’:
/* execute if opt == ’B’ || opt == ’C’ */
default:
}