Although the simple if statement that you saw in Sectionย 2.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)
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ย 2.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.
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.
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ย 2.3. However, you cannot have an else-block without a corresponding if-block.
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ย 2.5.
Yes
Correct! This will cause an error because the second else-block is not attached to a corresponding if-block.
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!