7.4. Conditional Execution: Binary Selection¶
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, sometimes 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.
x = 15
if x % 2 == 0:
print(x, "is even")
else:
print(x, "is odd")
(ch05_4)
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 indented statements get executed. If not, then the statements
indented under the else clause get executed.
As with the function definition from the last chapter and other compound
statements like for
, 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 first unindented statement marks the end of the block.
Each of the statements inside the first block of statements is executed in order if the boolean
expression evaluates to True
. The entire first block of statements
is skipped if 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.
Lab
- Approximating Pi with Simulation In this guided lab exercise we will work through a problem solving exercise related to approximating the value of pi using random numbers.
Check your understanding
select-4-2: What does the following code print (choose from output a, b, c or nothing).
if 4 + 5 == 10:
print("TRUE")
else:
print("FALSE")
select-4-3: What does the following code print?
if 4 + 5 == 10:
print("TRUE")
else:
print("FALSE")
print("TRUE")
a. TRUE
b.
TRUE
FALSE
c.
FALSE
TRUE
d.
TRUE
FALSE
TRUE