Skip to main content

Section 7.6 Chaining and Nesting

You can also make complex decisions by nesting. Nesting refers to placing one conditional statement inside another. For example, if we want to print if a number is positive, negative, or zero, we could do this:
if (x > 0) {
  cout << "x is positive";
} else {
  if (x < 0) {
      cout << "x is negative";
  } else {
      cout << "x is zero";
  }
}
If x is greater than 0, the initial if block runs and the entire else is skipped. But if x is not greater than 0, the initial else block runs. In it, we make a new decision and either execute the inner if or the inner else.
These kinds of nested structures are common, but they can become difficult to read very quickly. Good indentation is essential to make the structure (or intended structure) apparent to the reader.
When nested conditions are focusing on a series of related tests (like what value x has), there is an alternative way to write the nesting. This other way of writing the same logic is called chaining because it links a series of if and else blocks:
if (x > 0) {
    cout << "x is positive";
} else if (x < 0) {
    cout << "x is negative";
} else {
    cout << "x is zero";
}
It is important to recognize that this is really just a pair of nested conditionals. The second if is really the body of the first else. We have just written it on the same line as the else. This is just a styling convention, not a new language feature. The logic remains the same. Either we do the first if body, and then skip the else body (which includes the second if and its attached else) or we do the else that has us make a new choice.
Although it is functionally the same as nested code, most programmers prefer to read and write this form. It makes it clear that we are picking one of three related branches to execute. This form is also preferable to a series of if statements unconnected by else’s as it guarantees that only one of the blocks in the chain will get to execute.
These chains can be as long as you like. For example, this code determines someone’s letter grade. If their score is 90+, we print out A and all the remaining tests are completely skipped. Otherwise, we execute the next check-m-is their score 80+? As soon as one branch is true, all the remaining tests and their associated blocks are skipped. Note that the last else does not need an if - it is the default case that handles things when all the other tests fail:
Listing 7.6.1.
When writing something like that, you must be careful to order the tests so that no earlier test also matches the condition of a later test. Here is a flawed attempt at the grade logic. If someone’s grade is 82, that makes the first test true. So the first block will execute and print out β€œYou got a D”. Then all the rest of the conditionals will be skipped. Nothing will make execution skip ahead to the β€œright” block.
if (grade > 60) {
  cout << "You got a D";
} else if (grade > 70) {
  cout << "You got a C";
} else if (grade > 80) {
  cout << "You got a B";
} else if (grade > 90) {
  cout << "You got an A";
} else {
  cout << "You got an F";
}

Checkpoint 7.6.1.

What will print?
#include <iostream>
using namespace std;

int main() {
  int x = 0;
  if (x == 0) {
    cout << "Hey!" << endl;
  }
  else {
    if (x > 0) {
      cout << "Hi!" << endl;
    }
    else {
      cout << "Hello!" << endl;
    }
  }
}
  • Correct!
  • Remember that the program would only enter the "else" if x was not equal to 0.
  • Hello!
  • Remember that the program would only enter the "else" if x was not equal to 0.
  • Nothing will print.
  • Only one of the condtionals will execute, but something will print, regardless of which one it is.

Checkpoint 7.6.2.

What will print?
#include <iostream>
using namespace std;

int main() {
  int x = -4;
  if (x == 0) {
    cout << "Hey!" << endl;
  }
  else {
    if (x > 0) {
      cout << "Hi!" << endl;
    }
    else {
      cout << "Hello!" << endl;
    }
  }
}
  • Remember that the program would only enter the first "if" if x was equal to 0.
  • Remember that the program would only enter the nested "if" if x was greater than 0.
  • Hello!
  • Correct!
  • Nothing will print.
  • Only one of the condtionals will execute, but something will print, regardless of which one it is.
You have attempted of activities on this page.