LAB 4.1      if, if/else  and  if/else if  Statements

 

 

Copy and paste the following program into Visual Studio IDE.

 

 

 

// This program prints "You Pass" if a student's average is

// 60 or higher and prints "You Fail" otherwise

 

#include <iostream>

using namespace std;

 

int main()

{

float average;                    // holds the grade average

 

cout << "Input your average:" << endl;

cin >> average;

 

if (average > 60)

    cout << "You Pass" << endl;

 

if (average < 60)

    cout << "You Fail" << endl;

 

return 0;

}

 

 

 

Exercise 1: Run the program three times using 80, 55 and 60 for the average. What happens when you input 60 as the average? Modify the first if statement so that the program will also print "You Pass" if the average equals 60.

 

Exercise 2: Modify the program so that it uses an if/else statement rather than two if statements.

 

Exercise 3: Modify the program from Exercise 2 to allow the following five cate­gories: Invalid data (grade is either above 100 or below 0), "A" category (90-100), "B" category (80-89), "You Pass" category (60-79), "You Fail" category (0-59). 

 

(Print out the program and the running result for Exercise 3, and hand them in with the rest of the lab.

 

 Note: Please print out five copies of running result, one for each category.)

 

 

 

 


LAB 4.2      The switch Statement

 

 

Copy and paste the following program into Visual Studio IDE.

 

 

// This program illustrates the use of the switch statement.

 

#include <iostream>

using namespace std;

 

int main()

{

 

char grade;

 

cout << "What grade did you earn in Programming I ?" << endl;

cin >> grade;

 

switch( grade )            // This is where the switch statement begins

{

case 'A': cout << "an A - excellent work !" << endl;

         break;

case 'B': cout << "you got a B - good job" << endl;

         break;

case 'C': cout << "earning a C is satisfactory" << endl;

         break;

case 'D': cout << "while  D is passing, there is a problem" << endl;

         break;

case 'F': cout << "you failed - better luck next time" << endl

         break;

default: cout << "You did not enter an A, B, C, D, or F" << endl;

}

 

return 0;

}

 

 

Exercise 1: Add an additional switch statement before the existing switch statement that will print "YOU PASSED! " if the grade is "D" or better. If the grade is "F", this additional switch statement will not print any message. Use the sample run given below to model your output.

 

Sample Run 1:

 

What grade did you earn in Programming I ?

A

YOU PASSED!

an A - excellent work!

 

Sample Run 2:

 

What grade did you earn in Programming I ?

F

You failed - better luck next time

 

Sample Run 3:

 

What grade did you earn in Programming I ?

J

You did not enter an A, B, C, D, or F

 

 

Exercise 2: Rewrite the program using if / else if statements rather than the switch statements. Use a trailing else in your new version to correspond to the default case in the switch statement.

 

 

(Print out the program and the running results for both Exercise 1 and Exercise 2. Print out three copies of running results for each exercise corresponding to the three sample runs above.)