Branching

Dr Andy Evans

[Fullscreen]

Control Flow statements

  • Generally we don't want linear collections of instructions.
  • No user interaction.
  • Code / calculation pathway can't act on previous calculations.
  • Large amounts of repeated code.
  • In general we want to make control flow more sophisticated.

Control Flow statements

  • Branching: If-this-do-this.
  • Looping: Do this many times with slight differences.
  • Procedure calls: Do this with some new information, and return an answer.
  • Sockets: Read/write code from/to somewhere else.
  • Objects: Make a toolkit to do a specific job.
  • Libraries: Group toolkits for easy use.

if compound statement

  • Compound statements are one or more clauses, the inside of which must be indented if not all on one line.

    if condition:    # Clause header.
        # do this    # Suite of statements.
        # do this
    This line always done
  • or (rarer):

    if condition: do this; do this
    This line always done
  • Example
    if a < 10:
        print("a less than 10")
    print("a assessed")
  • or
    if (a < 10):
        print("a less than 10")
    print("a assessed")
  • Parentheses not needed, but can make things clearer, especially with multiple operators. Again, don't rely on precedence.

if-else

  • Either / or:
    if condition:
        # do this
        # do this
    else:
        # do this
        # do this
    This line always done
  • Example:
    if a < 10:
        print("a less than 10")
    else:
        print("a greater than 10")
    print("a assessed")
  • The if-else-if ladder
    if condition:
        # do this
        # do this
    elif condition:
        # do this
        # do this
    else:
        # do this
        # do this
    This line always done
  • Example
    if day <= 5:
        print("Weekday")
    elif day == 6:
        print("Saturday")
    else:
        print("Sunday")
  • But you have to watch for inefficiencies.

Nested compound statements

  • Example:
    if a < 10:
        if b < 10:
            print("a and b less than 10")
        else:
            print("b greater than 10, a less")
    print ("a and b assessed")
  • Note that to avoid ambiguity as to what the else links to, you can't do this all on one line.

Conditions

  • Best to think of these as having to evaluate to either True or False.
    a = 2 if (a == 2):    # True
    if (a != 2):    # False
    if (a != 3):    # True
    if not (a == 2):    # False

    a = True if (a):    # True
    if not (a):     # False
    a = False
    if (a):     # False
    if not (a):     # True
  • Combining with Boolean operators:
    if (a == 2) or (b == 3): # If a == 2 OR b == 3
    if (a == 2) and (b == 3): # If a == 2 AND b == 3

    OR and AND can therefore shortcut if the first condition is respectively true (for OR) or false (for AND).
  • Although this is possible:
    if not a is None:
    do instead:
    if a is not None:
  • Note that empty sequences are false, so this is recommended by the docs:
    if not seq:
    if seq:

Conditional quirks

  • This is possible:
    x < y < z     # Is (x < y) and (y < z).
    x < y > z     # Is fine.

Ternary operator

  • Means: x if condition; y if not condition.
    x if condition else y
  • For example:
    a = 10
    b = "less than 5" if a < 5 else "more than five"
    print(b)
  • The Python FAQ gives this nice example:
    x, y = 50, 25
    small = x if x < y else y