Back    home  Next          view contents    in a pop up window

 Go to:  if - if else - while / for / do  - <continue

 

The switch command.                              

                                                                                 

         In program 1.3 the program asked us about instructions. It wanted to know into what kind of weight system did we want to measure our weight. It asked us to chose between options by entering the appropriate letter.

       A more sufficient  way to do this, is by using the switch command. It’s a more effective way to write a program when you are dealing with decision making.

             It gives the user more control over the program.

It consists of cases. The switch command is a multiple selection structure.  Each case may contain one or more lines of instructions that are executed indepentanly  from the other cases. If a certain  condition is met the apropriate block of code within the case will be executed and then  break; . After the program has executed the instructions that are in the case, it will exit from it and it does that when it reads the break  command.  But lets see this in action.

program 1.5 

#include <iostream.h>

int main () 
{

int select;

cout << " Enter one number between 1 and 3:";
cin >> select;

switch (select) {                            // line 7

case (1):                                    //  line 8
cout << " You entered the number " <<  select << "\n" ;   // line 9
break;

case (2):                                   //line 11
cout << " You entered the number " << select << "\n";
break;

case (3):                                     //line 14
cout << " You entered the number " << select << "\n";
break;

}                                                              // line17

return 0;

}

output of program program 1.5

Enter one number between 1 and 3: 2
You entered the number 2
Press any key to continue

This program asks us to enter a number between one and three. We have set select as an int variable. Now instead of writing    if/else  statements on line 7 we use the switch statement. Notice the phrasing switch(select)  .    What this means is that, that the program is prepared to switch among cases according to our input. It is imperative to inlcude all cases within braces otheriwise the program will not work. Look at the braces in line 7 and 17. This is a format that is always used when using the switch command.

       Now we have to set up our cases. On line 8 we do this by writing case(1): . What this command means is that it will execute the fallowing command on line 8 only when we enter the number one. Line 8 actually expects the numerical value of  1 to execute the code underneath it.

 The same applies for line 11 and line 14. In line 11 the program expects the number 2 and in line 14 it expects the number 3.

   Now what about the break; command? As you see in the output snapshot of program 1.3, we have entered the number 2. The program then executed the cout  command  underneath it  ( line 9 ) . When this line was executed the program then continued to the next line for instructions and it read the command break;. This command “told” the program to exit because there is nothing more to be done.  The same pattern will occur in all three cases if we enter 1 or 3.

 

Lets see another example of the switch  command.

 

  program 1.6

#include <iostream.h>

int main () 
{

int num1, num2;            //line 4

char select;

cout << " Enter one one of the fallowing: \n";
cout << "A to preform addition\n";
cout << "S to preform subtraction\n";
cout << "M to preform multiplication\n";
cout << "D to preform division.\n";
cin >> select;

switch (select) 
{
case 'a':                            // line 14
case 'A':                           // line 15
int sum;
cout << " the computer will preform addition.\n";
cout << " Please enter two numbers:";
cin >> num1 >> num2;

sum=num1+num2;
cout << " the sum is:" << sum << endl;
break;

case 's':                //line 23
case 'S':               //line 24
int difference;
cout << " The computer will preform subtraction.\n";
cout << " please enter 2 numbers: ";
cin >> num1 >> num2;

difference=num1-num2;
cout << " The Difference is:" << difference << endl;
break;


case 'm':          // line 32
case 'M':         // line 33
int product;
cout << " The computer will preform multiplication.\n";
cout << " Please enter two numbers: ";
cin >> num1 >> num2;
product=num1*num2;
cout << " The product is: " << product << endl;
break;

case 'd':              // line 42
case 'D':             // line 43
float remainer;
cout << " The computer will preform division.\n";
cout << " Please enter 2 numbers: ";
cin >> num1 >> num2;
remainer=num1/num2;
cout << " The remainer is" << remainer << endl;
break;

default:
cout << " You have entered an invalid letter.";
break;
}

return 0;

}

outputs of program 1.6
Enter one one of the fallowing:
A to preform addition
S to preform subtraction
M to preform multiplication
D to preform division.
y
You have entered an invalid letter.Press any key to continue






Enter one one of the fallowing:
A to preform addition
S to preform subtraction
M to preform multiplication
D to preform division.
s
The computer will preform subtraction.
please enter 2 numbers: 10
15
The Difference is:-5
Press any key to continue





 

 

 

This   program as you can see asks us what kind of mathematical operation we want it to preform between two numbers. Each case as you see has its own program block that will give instructions to the computer on how to preform each mathematical operation.

           Important notice: Variables num1and num2 on line 4 must be placed outside the program block of the switch statement. If we were to set the same letters as variables in all cases, the compiler would give us a “redefinition error”. Of course you can set variables with in each case but they must not be alike with any other variable in the other cases.

        To manipulate each case we set select as our variable. Notice in lines 11 and 12 what we did: 

case ‘a’:

case ‘A’:

   We could have just written case’a’:. But writing it as we did in program 1.5 we prevent an error from occuring if the user inputs a capital ‘A’ instead of ‘a’.  This again is a good error handler. Notice this nice little trick in lines 14-15,  23-24, 32-33, 42-43.

    What would happen though if the user enters an invalid letter?  That’s where the default: command comes into play. If the user enters an invalid letter that does not correspond to any of the cases, the program instead of canceling out it will go to the default:  command and gracefully will inform the user that he has entered and invalid letter and then cancel out.

What you should do though, in case you want your program to loop instead of canceling out? After all you want to give the user as much control as possible over the program.

 

Lets now proceed on the  for-while-do selection structure that deal with loops.

    

 

Go to: if - if else - while - for continue

 

Back    home  Next         view contents    in a pop up window

  Go to:  if - if else - while / for / do  - <continue

 

1
Hosted by www.Geocities.ws

1