Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Section 4.4 Chained Conditionals

Sometimes you want to check for a number of related conditions and choose one of several actions. One way to do this is by chaining a series of ifs and elses:
Listing 4.4.1. The following program classifies a number (x) as positive, negative, or zero. Feel free to change the value of x to make sure it works.
Try changing the value of x above to see how the output is impacted.

Note 4.4.1.

If you have a chain of if statements, the program will go through executing each conditional, regardless if the conditions are met. However, as soon as you add an else or even an else if statement, the program will stop executing the chained conditionals as soon as a condition is met.
These chains can be as long as you want, although they can be difficult to read if they get out of hand. One way to make them easier to read is to use standard indentation, as demonstrated in these examples. If you keep all the statements and squiggly-braces lined up, you are less likely to make syntax errors and you can find them more quickly if you do.

Checkpoint 4.4.1.

What will print after the following code is executed?
#include <iostream>
using namespace std;

int main() {
  int x = 10;
  if (x > 8) {
    cout << "One! ";
  }
  if (x > 6) {
    cout << "Two! ";
  }
  if (x > 3) {
    cout << "Three!" << endl;
  }
  return 0;
}
  • Three!
  • Make note of the use of "if" instead of "else if" or "else".
  • One!
  • Make note of the use of "if" instead of "else if" or "else".
  • One! Two!
  • Make note of the use of "if" instead of "else if" or "else".
  • One! Two! Three!
  • When we have "if" statments, but no "else if" or "else", every condition will be checked.

Checkpoint 4.4.2.

What will print after the following code is executed?
#include <iostream>
using namespace std;

int main() {
  int x = 10;
  if (x > 8) {
    cout << "One! " ;
  }
  else if (x > 6) {
    cout << "Two! ";
  }
  else {
    cout << "Three!" << endl;
  }
  return 0;
}
  • Three!
  • Remember that only one action will be completed in a chain of "ifs", "else ifs", and "ifs"
  • One!
  • The chain of "ifs", "else ifs", and "elses" results in only one action being completed.
  • One! Two!
  • Remember that a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
  • One! Two! Three!
  • Remember that a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.

Checkpoint 4.4.3.

What will print after the following code is executed?
#include <iostream>
using namespace std;

int main() {
  int x = 7;
  if (x > 8) {
    cout << "One! " ;
  }
  if (x > 6) {
    cout << "Two! ";
  }
  if (x > 3) {
    cout << "Three!" << endl;
  }
  return 0;
}
  • Two!
  • Make note of the use of "if" instead of "else if" or "else".
  • Two! Three!
  • When we have "if" statments, but no "else if" or "else", every condition will be checked.
  • One! Two!
  • The first statement will not be executed because x > 8 is not true. Also, make note of the use of "if" instead of "else if" or "else".
  • One! Two! Three!
  • The first statement will not be executed because x > 8 is not true.

Checkpoint 4.4.4.

What will print after the following code is executed?
#include <iostream>
using namespace std;

int main() {
  int x = 7;
  if (x > 8) {
    cout << "One! " ;
  }
  else if (x > 6) {
    cout << "Two! ";
  }
  else {
    cout << "Three!" << endl;
  }
  return 0;
}
  • Two!
  • Only one action will is completed in a chain of "ifs", "else ifs", and "ifs";
  • Two! Three!
  • Remember that only one action will be completed in a chain of "ifs", "else ifs", and "ifs".
  • One! Two!
  • The first condition will not be satisfied. Also, a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
  • One! Two! Three!
  • hge first condition will not be satisfied. Also, a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
You have attempted 1 of 3 activities on this page.