Section 4.8 Nested conditionals
One conditional can also be nested within another. We could have written the three-branch example like this:
The outer conditional contains two branches. The first branch contains a simple statement. The second branch contains another if
statement, which has two branches of its own. Those two branches are both simple statements, although they could have been conditional statements as well.
Checkpoint 4.8.1.
Q-2: Will the following code cause an error?
x = -10
if x < 0:
print("The negative number ", x, " is not valid here.")
else:
if x > 0:
print(x, " is a positive number")
else:
print(x," is 0")
No
This is a legal nested if
-else
statement. The inner if
-else
statement is contained completely within the body of the outer else
block.
Yes
This is a legal nested if
-else
statement. The inner if
-else
statement is contained completely within the body of the outer else
block.
Although the indentation of the statements makes the structure apparent, nested conditionals become difficult to read very quickly if you nest them too deeply. In general, it is a good idea to have no more than two levels of nesting.
Logical operators often provide a way to simplify nested conditional statements. For example, we can rewrite the following code using a single conditional:
The first print
statement is executed only if we make it past both conditionals, so we can get the same effect with the and
operator:
There are, however, cases where a nested if
is very useful. This will usually happen when you have tests on two different variables (unlike the preceding example, where both conditions involve x
).
Consider the following program, which figures out a ticket price for a movie according to a customer’s age and the time at which the movie starts (using a 24-hour clock) Here are the rules:
For people less than 65 years old:
If the hour is less than 15 (3 p.m.), they pay $7
Otherwise (the movie starts at or after 3 p.m.) they pay $8
Everyone else pays $9.
Here is the program, using a nested if
. Run it several times, entering different values for age
and hour
to see what output you get.
Checkpoint 4.8.2.
Q-5: Which of I, II, and III below gives the same result as the following nested if?
# nested if-else statement
x = -10
if x < 0:
print("The negative number ", x, " is not valid here.")
else:
if x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
I.
if x < 0:
print("The negative number ", x, " is not valid here.")
else x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
II.
if x < 0:
print("The negative number ", x, " is not valid here.")
elif x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
III.
if x < 0:
print("The negative number ", x, " is not valid here.")
if x > 0:
print(x, " is a positive number")
else:
print(x, " is 0")
I only
You can not use a Boolean expression after an else
.
II only
Yes, II will give the same result.
III only
No, III will not give the same result. The first if
statement will be True, but the second will be False, so the else
part will execute.
II and III
No, Although II is correct, III will not give the same result. Try it.
I, II, and III
No, in I you can not have a Boolean expression after an else
.
Checkpoint 4.8.3.
Q-6: True or False? You can use elif
statements within nested if
-else
statements.
True
Yes, it is possible to use elif
statements within nested if
-else
statements. Just make sure you are keeping track of all the branches.
False
Try again. You can have multiple branches within each branch of an if
-else
statement.