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

In this lab we will:
Learn about Boolean Expressions and how they will be evaluated
Learn about the switch statement
Learn about for loop

PART (1)
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.

// ******* lab91.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 to convert the code given below so that it uses switch statement 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;
}

PART (2)
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 }.

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

cout << "Enter a letter A to E, and I will \n";
cout << "print the names that start with that letter \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: Find what the controlling statement is first.  That is what determines what direction your code will go, i.e., controls the flow of your code.
For Loop
This is the syntax for the for loop:
Syntax:
for(Intialization_Action; Boolean_Expression; Update_Action)
{
Statement 1;
Statement 2;
}

PART (3)
// ********* lab93.C *************************************
Convert the following code so that it uses for loop instead of while loop.

//The following code will read the data from an input file and computes the sum of each line.
// The input file has n rows and m columns;
// Example:  The following file has 3 rows 5 columns:
// 2 4 6 12 14
// 6 8 10 10 8
// 8 10 12 4 6

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>

int main( )
{
int i, j, x, sum_row;
int rows, columns;
ifstream in_s;
char inp_file[15];

cout << "Please enter the input file name \n";
cin >> inp_file;

in_s.open(inp_file);
if( in_s.fail()){
cout << " Error opening the input file \n";
exit(1);
}

cout << "Please enter number of rows \n";
cin >> rows;
cout << "Please enter number of columns \n";
cin >> columns;

i = 0; //Initialization_Action for i

while(i < rows){//Boolean_Expression (i<columns) is either true or false
j = 0; //Initialization_Action for j
sum_row = 0;
while(j < columns){ //Boolean_Expression (j < columns) is either true or false
in_s >> x;
sum_row = sum_row + x;
j++; //Update Action for 2nd while
}
cout << "Sum of all numbers in row " << i << " is : " << sum_row<<endl;
i++; //Update Action for the 1st while
}
in_s.close();
return 0;
}

By the way when you have one loop that runs within another one, we call it a nested loop.