Activity 7-2 - if ... else if ... else Statement
                   switch Statement



In Lab (2), you learned to change the flow of your program using if ... else statements.  The if ... else statement deals with situations where there are two options and you take one or the other depending on the outcome of the Boolean expression.  However, sometimes you may run into a situation where you have more than two choices.  Think about an elevator.  You enter an elevator and, if the building has 4 floors and a basement, like mine does, you have 5 choices.  For now we think of a simple elevator, but more sophisticated ones may also have a button to close or to open the door.  As you can imagine, if you enter an elevator and do not press a key, the door will close but you will not move anywhere.  So the default action is actually the close door action.  Of course, sometimes you may enter an elevator and do not push a button, then someone from another floor may press the button outside and lead the elevator to where he/she stands.  As you can see, the button outside an elevator is up or down and that is a good example of if ... else, but inside the elevator, you need a more sophisticated statement to control the flow.  We will use if ... else if ... else statement for inside.  Following is a program that simulates a simple elevator.

// P71.cpp - A simple elevator for 4 floors and a basement with a
// close door button and 5 keys for floors

#include<iostream>
#include<cmath>
using namespace std;

void close_door();
int change_floor(int choice, int floor);

int main( )
{
     int key;
     int floor = 0;

    cout << "Press a Key \n";
    cin >> key;

     if(key == 0)
     {
             close_door();
             if( floor != 0){ // otherwise nothing happens
                 cout << "I am moving to the Basement \n";
                 floor = change_floor(0, floor);
             }
      }
      else if(key == 1)
      {
             close_door();
             if( floor != 1){ // otherwise nothing happens
                 cout << "I am moving to the the First Floor \n";
                 floor = change_floor(1, floor);
             }
       }
      else if(key == 2)
      {
             close_door();
             if( floor != 2){ // otherwise nothing happens
                 cout << "I am moving to the the Second Floor \n";
                 floor = change_floor(2, floor);
             }
       }
      else if(key == 3)
      {
             close_door();
             if( floor != 3){ // otherwise nothing happens
                 cout << "I am moving to the the Third Floor \n";
                 floor = change_floor(3, floor);
             }
       }
      else if(key == 4)
      {
             close_door();
             if( floor != 4){ // otherwise nothing happens
                 cout << "I am moving to the the Fourth Floor \n";
                 floor = change_floor(4, floor);
             }
       }
      else
      {
             close_door();
       }

       return 0;
}

void close_door()
{
     cout << "I am closing the door \n";
}

int change_floor(int choice, int floor)
{
       int move;
       move = floor - choice;
       // Note floor = choice is already considered in main

       if(move < 0)
       {
               cout << "Move up by " << abs(move) << endl;
        }
        else
        {
               cout << "Mode down by " << abs(move) << endl;
        }

        floor = choice;
        cout << "You are at floor " << floor << endl;

        return floor;
}

Exercise 7.2
Write the above program using if and if ... else statements and call your new program ex71.cpp.  In other words, replace the if ... else if ... ... else statements with if or else statements.  Can you explain the advantage of using if ... else if ... else instead of if statements?

switch Statement
Switch statements can be used for multiway branches similar to the if ... else if ... else statement used in the previous example.  The syntax for a switch statement is given below:

switch (controlling_Expression)
{
          case constant_1:
                statement sequence for case 1
          break;
          case constant_2:
                statement sequence for case 2
          break;
          ....
          ....
          ....
          default:
                statement sequence for the default case
}

In program P71.cpp, the controlling_Expression is variable key, because its value will control which branch to execute.  In that program each if or else if statement is a separate case, and the else is the default case.  Note that if you use the switch statement, you need to end each case with a break statement.  If you leave a break out, then the next case will also be executed.  This is a way to write the statements with AND.  For example:
The following segment:

if( key == 1 && key == 2)
{
      ....
}

will be represented as:

case 1:
case 2:
       .....
break;

in a switch statement.

Exercise 7.3
Modify the P71.cpp program and use a switch statement instead of the if ... else if ... else statement in the main.  Call your new program ex73.cpp.