Section 3 - The if/else/if Block
3B.3.1 if/else ... if/else ...
A common control structure in C++ is one in which a number of mutually exclusive conditions are expected, each with its own consequences. By inserting else if between an if statement and an else statement, we get this effect - as soon as a condition in the if or one of the if/else lines is met, the statement or block it controls is executed, and the program continues by skipping to the end of the entire if/else chain:
#include <iostream> #include <string> using namespace std; int main() { int depth; cout << "What is your diving depth in feet? "; cin >> depth; if (depth < 33) cout << "Use 2 atm for your calculation.\n"; else if (depth < 66) cout << "Use 3 atm for your calculation.\n"; else if (depth < 99) cout << "Use 4 atm for your calculation.\n"; else if (depth < 132) cout << "Use 5 atm for your calculation.\n"; else cout << "You are down too deep!\n"; }
It looks as if they are not really mutually exclusive, since the < 33 case is "contained in" the < 66 case. But since we don't test < 66 unless the < 33 has failed, then the < 66 case is really going to catch numbers between 33 and 66, and not < 33.
As a technical matter, these are really a bunch of nested if/else statements:
if (depth < 33) cout << "Use 2 atm for you calculation. \n"; else { if (depth < 66) cout << "Use 3 atm for you calculation. \n"; else { if (depth < 99) cout << "Use 4 atm for you calculation. \n"; else { if (depth < 132) cout << "Use 5 atm for you calculation. \n"; else cout << "You are way too deep! \n"; } } }
However, showing this nesting does nothing to improve the understanding of the program. In fact, it obfuscates the true nature of our logic, which is to pigeon-hole a value. The above poor style, is called progressive indentation. It should not be used if all the various cases being tested have equal status. There is nothing "better" about the depth being in the 66-99 range than there is about it being in 33-66 range. Progressive indentation is used when there is some true nesting relationships in the logic. That will be evident in the next example, but you can ask me in the forums if you have any doubt.
Do not use progressive indentation like the last example, but rather even indentation (AKA linear or stacked indentation) like the first one. You will lose a point if you violate this rule, and neither you nor I want that!

If there is logic that is truly nested, then we would use progressive indentation, and perhaps braces, even when not needed:
if (state == 'y') { if (price < .12) tax = 0; else if (price < .24) tax = .01; else if (price < .36) tax = .02; else tax = price * .083; } else tax = 0; total = price + tax;
Here I have combined a linear if/else if with a nesting (progressive indentation). Once we know whether or not we are in-state, we can determine the tax. These are two separate questions and should be treated with nesting. However, if we are in-state, the questions of being < .24 or < .36 are part of a pigeon-holing process and should be linearly indented or stacked.