// P41.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 4.2
Write the above program using if and if ... else statements
and call your new program ex41.cpp. In other words, replace the if
... else if ... ... else statements with if or else statements. In the comment at the beginning of the prgroam, 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 P41.cpp, the controlling_Expression is the 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:
.....// the body here is executed both for case 1 and case 2
break;
in a switch statement.
Exercise 4.3
Modify the P41.cpp program and use a switch statement instead
of the if ... else if ... else statement in the main. Call
your new program ex43.cpp.