19.2.2.1. Exercises: Selection Statements

Be the computer and predict the output of Example 19.1. Then run it and compare your predicted output with the output produced by the computer.

[ fromfile: controlstructures.xml id: None ]

Example 19.1. src/early-examples/nestedif.cpp

#include <iostream>
using namespace std;

void nestedif1 () {
    int m = 5, n = 8, p = 11;
    if (m > n)
        if (p > n)
            cout << "red" << endl;
        else
            cout << "blue"  << endl;
}


void nestedif2() {
    int m = 5, n = 8,  p = 11;
    if (m > n) {
        if (p > n)
            cout << "red"  << endl;
    } else
        cout << "blue" << endl;
}


int main() {
    nestedif1();
    nestedif2();
    return 0;
}