Multiple Alternatives. The Switch Statement

The comments section of the program
/*  Example on switch statement
    Jacob Y. Kazakia
    Spring 2001
 
Engineering 1 is usually taught in many sections
We try to get the best people to conduct the recitations. This program provides the name of your instructor when you give the number of the section ( 10, 11, 12, 13, 14, 15, 16, 17)
*/
Start of the program
#include <iostream.h>
main()
{
The variable of the switch statement is declared and it is assigned a value.  This variable can be only of type integer or character.
int section;
 
cout<<" \n\n  Who is your instructor?";
cout<<"\n  Enter your section number   >>> ";
cin>> section;
The switch statement
switch(section)
{
The first block. If section has the value of the cases listed, the statements before the break will be executed.  All other statements up to the closing bracket of the switch statement are bypassed.
case 10:
case 11:
cout<<" \n Bob Hope"<< endl;
break ;
The second block. If section has the value of the cases listed, the statements before the break will be executed.  All other statements up to the closing bracket of the switch statement are bypassed.
case 12:
case 13:
cout<<" \n Science Dreamer"<< endl;
break;
The third block. If section has the value of the cases listed, the statements before the break will be executed.  All other statements up to the closing bracket of the switch statement are bypassed.
case 14:
case 15:
cout<<" \n He_thinks_too_much"<< endl;
break;
The fourth block. If section has the value of the cases listed, the statements before the break will be executed.  All other statements up to the closing bracket of the switch statement are bypassed.
case 17:
case 18:
cout<<" \n John Knowall"<< endl;
break;
If the value of the switch variable is not listed in the earlier four blocks, then the default block is activated
default:
cout<<"  \n  I hope you learn your section number soon";
 
}
This part is common to any of the above five paths.
cout<<" \n\n Have a nice day \n\n";
 
cout<<" enter e ( exit) to exit ....";
char hold;
cin>> hold;
 
}

 

© 2001 J.Y. Kazakia. All rights reserved