Comp 112

Lecture 4

Flow Control

2017.09.26

Conditional Execution

Alternative Execution

Chained Conditionals

Multiple Alternatives

We can add an else clause to a chained conditional:

if <first_condition> :
    <first_branch>
elif <second_condition> :
    <second_branch>elif <last_condition> :
    <last_branch>
else :
    <default_branch>

General Form of if Statements

The most general form of an if statement has:

Example: Guessing Game

Let’s think of something and have Python guess what it is.

def guessing_game () :
    if input ('is it an animal? ') == 'yes' :
        if input ('does it have legs? ') == 'yes' :
            print ('then it must be a centipede!')
        else :
            print ('then it must be a shark!')
    elif input ('is it a vegetable? ') == 'yes' :
        if input ('does your mom make you eat it? ') == 'yes' :
            print ('then it must be broccoli!')
        else :
            print ('then it must be a pumpkin!')
    elif input ('is it a mineral? ') == 'yes' :
        if input ('is it shiny? ') == 'yes' :
            print ('then it must be gold!')
        else :
            print ('then it must be mud!')
    else :
        print ('then I give up!')

Complex Conditionals

The branching structure of conditionals can be arbitrarily complex.

But complicated conditional statements can get confusing.

def leap_deep (year) :
    if year % 4 == 0 :
        if year % 100 == 0 :
            if year % 400 == 0 :
                print (str (year) + ' is a leap year')
            else :
                print (str (year) + ' is not a leap year')
        else :
            print (str (year) + ' is a leap year')
    else :
        print (str (year) + ' is not a leap year')

Boolean expressions are useful to simplify conditional structure.

def leap_flat (year) :
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 :
        print (str (year) + ' is a leap year')
    else :
        print (str (year) + ' is not a leap year')

Conditional Expressions

You can also write conditional expressions:

<true_expression> if <boolean_guard> else <false_expression>

like:

def min (x , y) :
    return x if x <= y else y

instead of:

def min (x , y) :
    if x <= y :
        return x
    else :
        return y

Repeated Execution

When control reaches a while statement:

Computing Mathematical Functions by Iteration

We can compute the factorial (mathematical) function (i.e. $n! = n × (n−1) × (n−2) × ⋯ × 1$) by iteration.

def factorial (n) :
    """
    signature: int -> int
    precondition: n >= 0
    returns the factorial of the argument
    """
    acc  =  1
    while n > 0 :
        acc  =  acc * n
        n  =  n - 1
    return acc

Augmented Assignment

Drawing Polygons using Iteration

def square (size) :
    sides_drawn  =  0
    while sides_drawn < 4 :
        turtle.forward (size)
        turtle.left (90.0)
        sides_drawn  +=  1

Validating User Input

Using loops and conditionals together we can validate user input:

To Do This Week: