If – Else Statements

 

Many things depend on the evaluation of boolean or logical expressions.  For example:

 

If it’s cloudy bring an umbrella.

 

If  I get an A in this course I’ll be happy.

 

If I get a C in this course I’ll be happy.

 

The general form of a C++ if statement:

 

if (boolean expression) statement;

 

The statement part is carried out only if the condition (boolean expression) evaluates to true.

 

Back to motivating problem:  Professor Imberman wishes to use a grading program to give students feedback on their work.  When a student enters his/her grade into the program, the program will provide the student with an appropriate comment assessing the student’s performance.  Students entering values of 98, 99, or 100 will receive the message “Great Work”.  All other grades lower than 98 will get the comment, “Better luck next time”.

 

Software Development Procedure:

 

Inputs – A grade

Output – Comment based on grades value

 

Extended Analysis

if grade is 98 or greater print “Great Work”

if it is less than 98 print “Better luck next time!”

 

Algorithm:

1.   Prompt user for grade

2.   Read in grade

3.   if grade >= 98 print “Great work”

4.   if it is less than 98 print “Better luck next time!”

 

#include <iostream>

using namespace std;

 

int

main()

{

     

      double grade;

 

      cout << "What was your grade on the latest exam?  " ;

      cin >> grade;

 

      if (grade >= 98) cout << "Great job!" << endl;

      if (grade < 98) cout << "Better luck next time" << endl;

 

      return 0;

}

 

What if we said for steps 7 and 8 in our algorithm:

if grade >= 98 print “Great work”

otherwise print “Better luck next time!”

 

if (boolean expression) statement;

else statement;

 

When the boolean expression is true, execute the statement following the if.  When the condition is false, execute the statement following the else. 

 

if (grade >= 98) cout << "Great job!" << endl;

      else cout << "Better luck next time" << endl;

 

Block if-else.

 

Now let’s expand this problem further.  If the grade is 90 and above we print, “Great job!” and set a character variable gradebookentry to an A, if it’s  if the grade is 89 or below, we print, “Please apply more effort!”, and set a character variable gradebookentry to an E.  For this we can use a block if-else.

 

Up unitl now our if statements could only execute one statement based on the truth value of the condition.  Here we need to execute more than one statement.

 

We do this by enclosing the statements we wish to execute in a set of curly brackets.  The syntax is as follows:

 

if (condition)

{       

          stmt 1;

          stmt 2;

            .

            .

            .

          stmt n;

}

 

So for our example….

 

#include <iostream>

using namespace std;

 

int main()

{

      double grade;

      char gradebookentry;

 

      cout << "What was your grade on the latest exam?  " ;

      cin >> grade;

 

      if (grade >= 90)

      {

            cout << "Great job!" << endl;

            gradebookentry = 'A';

      }

 

      if (grade <= 89)

      {

            cout << "Please apply more effort!"  << endl;

            gradebookentry = 'E';

      }

     

      cout << "Your grade so far is:  " << gradebookentry << endl;

      return 0;

}

 


The else portion of the if statement can also contain a multiple block of statements.  The syntax is:

 

if (condition)

{       

          stmt 1;

          stmt 2;

            .

            .

            .

          stmt n;

}

else

          stmt 1;

          stmt 2;

            .

            .

            .

          stmt n;

}

 

#include <iostream>

using namespace std;

 

int main()

{

      double grade;

      char gradebookentry;

 

      cout << "What was your grade on the latest exam?  " ;

      cin >> grade;

 

      if (grade >= 90)

      {

            cout << "Great job!" << endl;

            gradebookentry = 'A';

      }

 

      else

      {

            cout << "Please apply more effort!"  << endl;

            gradebookentry = 'E';

      }

     

      cout << "Your grade so far is:  " << gradebookentry << endl;

      return 0;

}

if (a > b)

{

          if (b > c)

cout << "Hi" << endl;

}

else cout << "Hello" << endl;

cout << "Good bye" << endl;

 

 

if (a > b)

          if (b > c)

cout << "Hi" << endl;

else cout << "Hello";

cout << "Good bye" << endl;

 

What is printed by the two code fragments if a = 10, b = 5 and c = 2?

if a = 5 b = 10 c = 2?

 

Each else statement matches the nearest preceding if statement.

 

if (a < b) if (b > c) cout << 'A';

else if (a > d) cout <<'B';

else if (d > a) cout << 'C';

else cout << 'D';

 

The above can be rewritten as:

 

if (a < b)

if (b > c)

          cout << 'A';

else

if (a > d)

cout <<'B';

else

if (d > a)

          cout << 'C';

else

          cout << 'D';

 

Problem:  Have 3 variables a, b, c.  We wish to find the variable that has the highest value and set another variable, max to that value.  We then print an appropriate statement.

 

#include <iostream.h>

 

int

main()

{

          int a, b, c;

 

          int max;

 

          cout << "Please enter 3 integers: ";

          cin >> a >> b >> c;

 

          if (a > b)

          {

                   if (a > c)

                             max = a;

                   else max = c;

          }

          else

                   if (b > c) max = b;

                   else max = c;

 

          cout << "The largest number is: " << max << endl;

 

          return 0;

 

}

 

if - else Chain

 

Problem: Quiz 2 was given.  We want to send a message to students based on their grades.  Write an if-else fragment that does the following:

 

90 +  Great Work

80 - 89  Good Job!

70 - 70  Good Try!

60 - 69  Study Harder

59 and below - Tutoring is available

 

if (grade > 90)  cout <<" Great Work" ;

else if (grade > 80) cout << "Good Job";

else if (grade > 70)  cout << "Good Try!";

else if (grade > 60) cout << "Study Harder";

else cout << "Tutoring is available";

 

 

Other formatting:

 

include <iostream>

using namespace std;

 

int

main()

{

      int grade;

 

      cout << "Please enter a grade: ";

      cin >> grade;

 

      if (grade >= 90) cout <<" Great Work" << endl;

      else if (grade >= 80) cout << "Good Job" << endl;

      else if (grade >= 70) cout << "Good Try!" << endl;

      else if (grade >= 60) cout << "Study Harder" << endl;

      else cout << "Tutoring is available" << endl;

     

      return 0;

}

 

In an if-else chain, the statement associated with the first true condition is executed.  The remainder of the chain is ignored.

 


Problem: Write a C++ program that will prompt a user for a year,  determine if a given year is a leap year, and then print an appropriate statement.  A year which is divisible by 4 and NOT divisible by 100 is a leap year.

 

#include <iostream>

using namespace std;

 

int

main()

{

      int year;

 

      cout << "Please enter a year ";

      cin >> year;

 

      if (year % 400 == 0) cout << "leap year" << endl;

      else if (year % 100 == 0) cout << "not leap year" << endl;

      else if (year % 4 == 0) cout << "leap year" << endl;

      else cout << "not leap year" << endl;

 

      return 0;

}

 

Using if's to filter data

 

The CSI dating service:

 

if (sex == 'M')

          if (age > 22 && age < 35)

                   if (workout == 'Y')

                             if (hasalotofmoney == 'Y')

                                      if (eyes == "blue")

                                                cout << "MINE";

 

The above construct tests for a set of true values.

 

Alternately:

 

if (sex == 'M' &&  age > 22 && age < 35 && workout == 'Y' &&

hasalotofmoney == 'Y' && eyes == "blue")

          cout << "MINE";