Skip to content

7.4/5/6/7 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 (Conditional Statements)

if statement

Structure of an if statement

if BOOLEAN EXPRESSION:
    STATEMENTS_1        # executed if condition evaluates to True
else:
    STATEMENTS_2        # executed if condition evaluates to False

Example

x = 15

if x % 2 == 0:
    print(x, "is even")
else:
    print(x, "is odd")

Unary Selection

  • The elseclause is omitted entirely
  • When the condition evaluates to True, the statements are executed.
  • Otherwise the flow of execution continues to the statement after the body of the if
  • Example:
x = 10
if x < 0:
    print("The negative number ",  x, " is not valid here.")
print("This is always printed")

7.6 Nested Conditionals

  • One conditional can be nested within another.
if x < y:
    print("x is less than y")
else:
    if x > y:
        print("x is greater than y")
    else:
        print("x and y must be equal")

7.7 Chained Conditionals

if x < y:
    print("x is less than y")
elif x > y:
    print("x is greater than y")
else:
    print("x and y must be equal")
  • Exactly one branch will be executed.
  • elifis an abbreviation of else if
  • There is no limit of the number of elifstatements
  • only a single (and optional) final elsestatement is allowed and it must be the last branch in the statement.
  • Each condition is checked in order
  • If the first is false, the next is checked, and so on, until one is true, branch executes, then conditional is over.