Control Flow

Control Flow Cont.

Looping with while and for

Control Flow

while Loop

The while loop is used to repeatedly execute a block of code as long as a condition is true.

Syntax

while condition:
    statement
Control Flow

Example

count = 0
while count < 5:
    print("Count:", count)
    count += 1
Control Flow

for Loop

The for loop is used to iterate over a sequence (such as a list, tuple, string, or range).

Syntax

for variable in sequence:
    statement
Control Flow

for Loop Examples

>> for i in range(4):
>>     print("Iteration:", i)
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3

>> for i in range(2, 4):
>>     print("Iteration:", i)
Iteration: 2
Iteration: 3
Control Flow

Less Common Flow Controll

Tune out if you're feeling overwhelmed.

Control Flow

break Statement

The break statement is used to exit a loop prematurely.

Example

for i in range(10):
    if i == 5:
        break
    print("Iteration:", i)
Control Flow

continue Statement

The continue statement is used to skip the rest of the code inside the loop for the current iteration only, and continue with the next iteration of the loop.

Example

for i in range(10):
    if i % 2 == 0:
        continue
    print("Odd number:", i)
Control Flow

pass Statement

The pass statement is a null operation; nothing happens when it executes. It is used as a placeholder.

Example

for i in range(10):
    if i % 2 == 0:
        pass
    else:
        print("Odd number:", i)
Control Flow

..., A Special Placeholder

... is used the same way as pass, but it's really more like None. It's a singleton that does nothing.

I will use it when I want to indicate that the code is not complete.

Example

for i in range(10):
    if i % 2 == 0:
        ...
    else:
        print("Odd number:", i)
Control Flow

Exercise: The Guessing Game

http://gg.gg/1b767l

TODO: Future note, this really should be covered right after while-loops. Integral part of them.