By Stephen R. Davis

In C++, the braces of an if or an else clause can contain another if statement. These are known as nested if statements. The following NestedIf program shows an example of a nested if statement in use.

// NestedIf - demonstrate a nested if statement
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
    // enter your birth year
    int  nYear;
    cout << "Enter your birth year: ";
    cin  >> nYear;
    // Make determination of century
    if (nYear > 2000)
    {
        cout << "You were born in the 21st century"
             << endl;
    }
    else
    {
        cout << "You were born in ";
        if (nYear < 1950)
        {
            cout << "the first half";
        }
        else
        {
            cout << "the second half";
        }
        cout << " of the 20th century"
             << endl;
    }
    // wait until user is ready before terminating program
    // to allow the user to see the program results
    cout << "Press Enter to continue..." << endl;
    cin.ignore(10, 'n');
    cin.get();
    return 0;
}

This program starts by asking for the user’s birth year. If the birth year is later than 2000, then the program outputs the string “You were born in the 21st century”.

In mathematically accurate terms, the year 2000 belongs to the 20th century, not the 21st.

If the birth year is not greater than 2000, then the program enters the else clause of the outer if statement. This clause starts by outputting the string “You were born in” before comparing the birth year to 1950.

If the birth year is less than 1950, then the program adds the first “the first half”. If the birth year is not less than 1950, then the else clause of the inner if statement is executed, which tacks on the phrase “the second half”. Finally, the program adds the concluding phrase “of the 20th century” to whatever has been output so far.

In practice, the output of the program appears as follows for three possible values for birth year. First, 2002 produces the following:

Enter your birth year: 2002
You were born in the 21st century
Press Enter to continue . . .

For example, 1956 generates the following:

Enter your birth year: 1956
You were born in the second half of the 20th century
Press Enter to continue . . .

Finally, the birth year of 1932 generates the third possibility:

Enter your birth year: 1932
You were born in the first half of the 20th century
Press Enter to continue . . .

You could use a nested if to avoid the unnecessary comparisons in the NestedBranchDemo program:

if (nOperand1 > nOperand2)
{
    cout << "Argument 1 is greater than argument 2"
         << endl;
}
else
{
    if (nOperand1 < nOperand2)
    {
        cout << "Argument 1 is less than argument 2"
             << endl;
    }
    else
    {
        cout << "Argument 1 is equal to argument 2"
             << endl;
    }
}

This version performs the first comparison just as before. If nOperand1 is greater than nOperand2, this snippet outputs the string “Argument 1 is greater than argument 2”. From here, however, control jumps to the final closed brace, thereby skipping the remaining comparisons.

If nOperand1 is not greater than nOperand2, then the snippet performs a second test to differentiate the case that nOperand1 is less than nOperand2 from the case that they are equal in value.

The figure shows graphically the flow of control for the NestedBranchDemo program for the input of 5 and 10.

image0.jpg

Performing the test for equality is unnecessary: If nOperand1 is neither greater than nor less than nOperand2, then it must be equal.