Although the simple if statement that you saw in Section 4.3 is useful, it only takes an action when the condition is True. Otherwise, it does nothing. In most programs, it is more common to have a “two-way” decision such as the following (phrased in English)
If your age is greater than 65, you get a 15% discount; otherwise, you get a 5% discount.
If the amount is greater than zero and less than the balance, subtract the amount from the balance; otherwise, tell the customer the amount is wrong.
If the year is a leap year, set the number of days for February to 29; otherwise set the nuber of days to 28.
To handle these decisions, we use a second form of the if statement: alternative execution, in which there are two possibilities and the condition determines which one gets executed. The syntax looks like this:
If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays a message to that effect. If the condition is false, the second set of statements is executed. Figure 4.4.1 shows the flowchart for this code.
Since the condition must either be true or false, exactly one of the alternatives will be executed. The alternatives are called branches, because they are branches in the flow of execution.
Checkpoint4.4.2.
Q-2: What does the following code print?
if 4 + 5 == 10:
print("WEIRD")
else:
print("GREAT")
WEIRD
WEIRD is printed by the if-block, which only executes when the conditional (in this case, 4 + 5 == 10) is true. However, 5 + 4 is not equal to 10, so the condition is False.
GREAT
Since 4 + 5 == 10 evaluates to False, Python will skip over the if block and execute the statement in the else block.
WEIRD on one line and GREAT on the next
Python would never print both WEIRD and GREAT because it will only execute either the if-block or the else-block, but not both.
Nothing will be printed
Python will always execute either the if-block (when the condition is true) or the else-block (when the condition is false). It will never skip over both blocks.
Checkpoint4.4.3.
Q-3: What does the following code print?
x = -10
if x < 0:
print("The negative number ", x, " is not valid here.")
print("This is always printed")
a.
This is always printed
b.
The negative number -10 is not valid here
This is always printed
c.
The negative number -10 is not valid here
Output a
Because -10 is less than 0, Python will execute the body of the if-statement here.
Output b
Python executes the body of the if-block as well as the statement that follows the if-block.
Output c
Python will also execute the statement that follows the if-block because it is not enclosed in an else-block, but rather just a normal statement. Notice also that print("This is always printed") is not indented.
It will cause an error because every if must have an else clause.
It is valid to have an if-block without a corresponding else-block, as we saw in an example in the preceding section 4.3. However, you cannot have an else-block without a corresponding if-block).
Checkpoint4.4.4.
Q-4: Will the following code cause an error?
x = -10
if x < 0:
print("The negative number ", x, " is not valid here.")
else:
print(x, " is a positive number")
else:
print("This is always printed")
No
Every else-block must have exactly one corresponding if-block. If you want to chain if-else statements together, you must use the elif construct, described in Section 4.5.
Yes
Correct! This will cause an error because the second else-block is not attached to a corresponding if-block.
Checkpoint4.4.5.
The following program should print out “x is even” if the remainder of x divided by 2 is 0 and “x is odd” otherwise, but the code is mixed up. Be sure to indent correctly!
x = 92
---
if x % 2 == 0:
---
print("x is even")
---
else:
---
print("x is odd")