If Statement

The If statement allows the programmer to change the logical order of a program; that is, make the order in which the statements are executed differ from the order in which they are listed in the program.

If-Then Statement

The If-Then statement uses a Boolean expression to determine whether to execute a statement or to skip it. Here is the syntax template:
if (Expression)

Statement
The expression in parentheses can be of any simple data type. Almost without exception, this will be a logical (Boolean) expression; if not, its value is implicitly coerced to type bool(nonzero value means true, zero value means false).

Now let's look at the following statement:


    int number, sum;
    sum = 10;
    cout << "Please enter an integer value: " << endl;
    cin >> number;
    if (number < 0)
        number = 0;

    sum = sum + number;
    cout << "The sum is " << sum << endl;

The expression (number < 0) is evaluated. If the result is true, the statement number = 0; is executed. If the result is false, the statement is skipped. In either case, the next statement to be executed is sum = sum + number;.

If-Then-Else Statement

If-Then-Else statement uses a Boolean expression to determine which one of the two statements to execute. Here is the syntax template:
if (Expression)

Statement_A else
Statement_B
The expression in parentheses will be evaluated with the result of true or false. Here is an example:
    cout << "You are ";
    if (age >= 65)
       cout << "a senior ";
    else
       cout << "not a senior ";
    cout << "citizen." << endl;
The characters "You are " are sent to the output stream. The expression age >= 65 is evaluated. If the result is true, the characters "a senior " are sent to the output stream. If the result is false, the characters "not a senior " is sent to the output stream. In either case, the next statement to be executed sends the the characters "citizen." to the output stream.

There is one thing to point out here: any statement in an IF or ELSE statement could be a block or a compound statement. If so, they must be enclosed in a pair of braces. Here is an example which also shows a compound expresssion:

    if ( (age > 65) && (gender == 'f') )
    {
        cout << "Quilting group meets:";
        cout << "Thursday afternoons at 4:00 p.m.";
    }

Nested If Statement

An If-Then statemenet uses Boolean expression to determine whether to execute or skip a statemenet. An If-Then-Else statement uses a Boolean expression to determine which one of the two statements to execute. The statements to be executed or skipped could be simple statements or compound statements (blocks). They also can be an If Statement. An If statement within another If statement is called a nested If statement.

A look-ahead: You will soon be learning about the C++ SWITCH statement. For very complex if/else constructs, it is preferable to use the Switch instead.

The following example is a nested If statement.

    cout << "You are ";
    if (age >= 65)
       cout << "a senior." << endl;
    else
       if (age >= 19)
          cout << "an adult." << endl;
       else
          if (age >= 13)
         cout << "a teenager." << endl;
          else
         cout << "a child." << endl;
    cout << "You are a great person." << endl;
The characters "You are " are sent to the output stream. The expression age >= 65 is evaluated. If the result is true, the characters "a senior." are sent to the output stream. If the result is false, the expression age >= 19 is evaluated. If the result is true,the characters "an adult." are sent to the output stream. If the result is false, the expression age >= 13 is evaluated. If the result is true,the characters "a teeneager." are sent to the output stream. If the result is false, the expression "a child. is sent to the output stream. In any case above, the next statement to be executed sends the the characters "You are a great person." to the output stream.
Notice: once age has a value, only one statement is selected to be executed. If we add braces to the program segment, it would be easy to read. Let's look at it now:
    cout << "You are ";
    if (age >= 65)
    {
       cout << "a senior." << endl;
       cout << "*****" << endl;
    }
    else
       if (age >= 19)
       {
          cout << "an adult." << endl;
          cout << "*****" << endl;
       }
       else
          if (age >= 13)
          {
            cout << "a teenager." << endl;
            cout << "*****" << endl;
          }
          else
	  {
            cout << "a child." << endl;
            cout << "*****" << endl;
	  }

    cout << "You are a great person." << endl;

Another Example


#include <iostream>
using namespace std;

int main() 
{
    char grade;

    cout << "Please enter a letter grade (A, B, C, D, or F): " << endl;
    cin >> grade;

    if (grade == 'A')
       cout << "Great work. " << endl;
       else if (grade == 'B')
            cout << "Good work. " << endl;
            else if (grade == 'C')
                 cout << "Passing work. " << endl;
                 else if (grade == 'D' || grade == 'F')
                 {
                    cout << "Unsatisfictory work. " << endl;
		    cout << "See your instructor." << endl;
		 }
		 else
                    cout << grade << " is not a legal grade." << endl;

    cout << endl;
    return 0;
} // end main


Copyright: Department of Computer Science, University of Regina.