Conditional Execution: Binary SelectionΒΆ




(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

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.

Check your understanding

select-4-1: 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





Next Section - Omitting the else Clause: Unary Selection