Section 4.2 Conditional Execution: Binary Selection
Now that we know how to express “True or False” questions in Python, it’s time to put them to use. In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Selection statements, also referred to as conditional statements, give us this ability. The simplest form of selection is the if statement. This is sometimes referred to as binary selection since there are two possible paths of execution.
The syntax for an if
statement looks like this:
if BOOLEAN EXPRESSION:
STATEMENTS_1 # executed if condition evaluates to True
else:
STATEMENTS_2 # executed if condition evaluates to False
The boolean expression after the if
statement is called the condition. If it is true, then the immediately following indented statements get executed. If not, then the statements indented under the else
clause get executed.
The if
statement consists of a header line and a body. The header line begins with the keyword if
followed by a boolean expression and ends with a colon (:).
The indented statements that follow are called a block.
The statements inside the first block of statements are executed when the boolean expression evaluates to True
. The entire first block of statements is skipped when the boolean expression evaluates to False
, and instead all the statements under the else
clause are executed.
There is no limit on the number of statements that can appear under the two clauses of an if
statement, but there has to be at least one statement in each block.
We will see more compound statements later in this course. In general, all compound statements include a heading and all the following further-indented statements in the block after the heading.
Checkpoint 4.2.1.
How many statements can appear in each block (the if
and the else
) in a conditional statement?
Just one.
- Each block may also contain more than one.
Zero or more.
- Each block must contain at least one statement.
One or more.
- Yes, a block must contain at least one statement and can have many statements.
One or more, and each must contain the same number of statements.
- The blocks may contain different numbers of statements.
Checkpoint 4.2.2.
What does the following code print (choose from output a, b, c or nothing)?
if 4 + 5 == 10:
print("Math is broken")
else:
print("Math is correct")
Math is broken
- “Math is broken” is printed by the
if
block, which only executes if the conditional (in this case, 4 + 5 == 10) is true. However, 4 + 5 is not equal to 10.
Math is correct
- Since 4 + 5 == 10 evaluates to
False
, Python will skip over the if
block and execute the statement in the else
block.
“Math is broken” on one line and “Math is correct” on the next
- Python would never print both sentences 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 would never skip over both blocks.
Checkpoint 4.2.3.
What does the following code print?
if 5 % 2 == 1:
print("odd")
else:
print("even")
print("finished")
a.
finished
b.
odd
finished
c.
odd
even
finished
Output a
Although finished
is printed after the if
-else
statement completes, both blocks within the if
-else
statement print something too. For this choice to be correct, Python would have to skip both blocks in the if
-else
statement, which it never would do.
Output b
Python will print odd
from within the else
block (because 5 % 2 does equal 1), and then print finished
after the if
-else
statement completes.
Output c
To print these three lines, Python would have to execute both blocks in the if
-else
statement, which it can never do.