Lab (8) - Boolean Expressions, Multiway Branches, and Switch Statement

In this lab we will:
Learn about Boolean Expressions and how they will be evaluated

Boolean Expressions:
We have learned so far that in C++, a Boolean expression evaluates to True (1) or False (0).  For example in the following code segment, we have used an if statement that does something when the conditional statement is True and another thing when that statement is False.

char c;

cout << "Enter a character and I will determine whether you have said yes or something else \n";
cin >> c;

if( c == 'y' || c == 'Y' ){
cout << "You have entered y or Y, for YES \n";
cout << "For OR, ||, we only need to have one part to be true, \n";
cout << "then the whole thing will be true \n";
}
else{
cout << "I know that you haven't entered y or Y, but whatever \n";
cout << "you have entered, means NO to me \n";
cout << "not that the if statement will get you here, only \n";
cout << "if you fail to enter y or Y.  That is how OR evaluate to False, \n";
cout << "when both parts are wrong \n";
}

The ( c == 'y' || c == 'Y' ) will evaluate to True or False.  In this example we have an OR (||) so this statement will evaluate to False, if both c == 'y' and c == 'Y' are false.  In that case, c is not 'y' and c is not 'Y' either.  Any other combination of these two will evaluate to True.  Please refer to Display 7.1 of the textbook for more information.

Note that in if( c == 'y' || c == 'Y' ){, we haven't put c == 'y' and c == 'Y' in parenthesis. This is because the Precedence Rules will set the order of operation, thus, these two will be evaluated before ||.  Please see Display 7.2 of the textbook for more information on Precedence Rules.

Following is another example for Boolean expression.  Copy this code to a file in your directory.  But, before you run the code, write on a piece of paper, the answers that you think will be generated.  Also answer the following two questions before you run the code.

Explain the difference between the way h and i are running?
Explain the difference between the way j and k are running?

Then, compile and run the code, lab81.C, to see what the outcome is.

// ******* lab81.C **************
#include<iostream.h>
int main()
{

int count = 0, limit = 10;
int x,y;

cout << "a  " << ( (count == 0) && (limit < 20)) << "\n";
cout << "b  " << ( count == 0 && limit < 20 ) << "\n";
cout << "c  " << ( (limit > 12) || (count < 5) ) << "\n";
cout << "d  " << ( !(count == 5) ) << "\n";
cout << "e  " << ( (count == 1) && (x < y) ) << "\n";
cout << "f  " << ( (count < 10) || (x < y) ) << "\n";
cout << "g  " << ( !( ((count < 10) || (x < y)) && (count >= 0)) ) << "\n";
cout << "h  " << ( ((limit/count) > 7) || (limit < 20) ) << "\n";
cout << "i  " << ( (limit < 20) || ((limit/count) > 7) ) << "\n";
cout << "j  " << ( ((limit/count) > 7) && (limit < 0) ) << "\n";
cout << "k  " << ( (limit < 0) && (( limit/count) > 7) ) << "\n";
cout << "L  " << ( (5 && 7) + (!6) ) << "\n";

return 0;
}

The switch statement
Using the following syntax for the switch statement, convert the code given below so that it uses switch instead of if statement.

Syntax for the switch statement:

switch( controlling statement)
{
case const_1:
statement sequence_1;
break;
case const_2:
statement sequence_2;
break;
....
...
..
case const_n:
statement sequence_n;
break;
default:
default statement sequence;
}

In the above syntax, you will see that every case has been started with case .... :, and has been closed with a break as opposed to { and } that we have seen in for loop, while loop, and if ...  The body of the switch statement, is whatever comes between { and }.

// ********* lab82.C *************************************
#include<iostream.h>
int main()
{
char c;

cout << "Enter a letter A to E, and I will \n";
cin >> c;

if( c == 'A')
{
cout << "Atkins, Tom \n";
cout << "Ashe, Nina \n";

}
else if ( c == 'B')
{
cout << "Blair, Bob \n";
}
else if ( c == 'C')
{
cout << "Cornel, Tania \n";
}
else if ( c == 'D')
{
cout << "Davis, James \n";
cout << "Durham, kim \n";
cout << "Duck, Alison \n";
}
else if ( c == 'E')
{
cout << "Eagle, Kathy \n";
cout << "Edgeworth, Tom \n";
}
else
{
cout << "Sorry, you have entered an invalid choice \n";
cout << "Better luck next time \n";
}
}

In the above code, could we use uppercase and lowercase interchangeably?

Hint on conversion of the above code: controlling statement is c.  That is what determines what direction your code will go, i.e., controls the flow of your code.