4.5. Nested Conditionals¶
In addition to chaining, you can also nest one conditional within another. We could have written the previous example as:
This program classifies a number (x) as positive, negative, or zero, just like the program on the previous page. However, this time, we are using nested conditionals.
There is now an outer conditional that contains two branches. The first branch contains a simple output statement, but the second branch contains another if statement, which has two branches of its own. Fortunately, those two branches are both output statements, although they could have been conditional statements as well.
Note
There is not a limit to the number of times you can nest a conditional. However, you should try to limit this number, as it will reduce the complexity of your program structure.
Too many nested conditionals is considered a bad practice.
Notice again that indentation helps make the structure apparent, but nevertheless, nested conditionals get difficult to read very quickly. In general, it is a good idea to avoid them when you can.
On the other hand, this kind of nested structure is common, so it is important to be able read nested code, regardless of whether it is a best practice or not.
- Hey!
- Correct!
- Hi!
- 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.
Q-2: What will print?
#include <iostream>
int main () {
int x = 0;
if (x == 0) {
std::cout << "Hey!\n";
} else {
if (x > 0) {
std::cout << "Hi!\n";
} else {
std::cout << "Hello!\n";
}
}
}
- Hey!
- Remember that the program would only enter the first "if" if x was equal to 0.
- Hi!
- 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.
Q-3: What will print?
#include <iostream>
int main () {
int x = -4;
if (x == 0) {
std::cout << "Hey!\n";
} else {
if (x > 0) {
std::cout << "Hi!\n";
} else {
std::cout << "Hello!\n";
}
}
}
- Back Left!
- Remember that the > opearator is not inclusive.
- Back Right!
- z > m is true, and m > m is false, so a student with these initials would be seated in the back right.
- Front Left!
- z > m is true because z comes after m. Also, the > opearator is not inclusive.
- Front Right!
- z > m is true because z comes after m.
- Error!
- Character comparisons are legal, and useful in this case!
Q-4: Your school uses a system to arrange students in a large stadium using their initials. Look at the function definition below. Where would a student with the initials “MZ” be seated?
string seating_arrangement(char first, char last) {
constexpr char center = 'm';
if (last > center) {
if (first > center) {
return "Back Left!";
} else {
return "Back Right!";
}
} else {
if (first > .) {
return "Front Left!";
} else {
return "Front Right!";
}
}
}
More to Explore
if from cppreference.com
The ‘Arrow anti-pattern’ from the Portland Pattern Repository (the very first wiki!)
Flattening arrow code from Jeff Atwood’s blog.