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.
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, and we will see it again, so you better get used to it.
- 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>
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;
}
}
return 0;
}
- 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>
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;
}
}
return 0;
}
- 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 seatingArrangement(char first, char last) {
if (last > m) {
if (first > m) {
return "Back Left!";
}
else {
return "Back Right!";
}
}
else {
if (first > m) {
return "Front Left!";
}
else {
return "Front Right!";
}
}
}