Flow Control
Overview
Teaching: 30 min
Exercises: 10 minQuestions
How can you affect the flow of execution in python?
Objectives
while, try, for, if/then/else can direct the flow of execution
In arduino programming all of the operation was contained in two functions: one called “setup” that was called once, and one called “loop” that was called over and over again.
In python, commands don’t need to be enclosed in functions: they simply get executed in order and the program will exit when it reaches the end. You can replicate the functionality of the arduino “loop” function by using “while”.
The while command takes a test as an argument and as long as that test evaluates as “true”, the progam will execute the steps inside the while loop as rapidly as possible. The simplest test that will evaluate as “true” is to just say “True.”
Before executing the next program, be ready to type a Ctrl-C to stop execution.
#! /usr/bin/env python3
while True:
print("Hello world!")
In this example, the program will output forever until interrupted. A Ctrl-C will do it. But notice that this stops execution ungracefully, wherever the program was, it just stops with no cleanup.
If you use “try”, you can catch errors or interrupts and clean up before exiting. This becomes more important if you’re opening files, writing to disk, or using serial connections all of which you probably should shut down gracefully rather than leaving to chance.
#! /usr/bin/env python3
try:
while True:
print("Hello world!")
except:
print("Done!")
In this example, you should be able to type Ctrl-C and see the statement “Done!” to show
This particular structure works like “loop” in Arduino – that is, it just loops forever. But you can also look only while certain conditions are true. But if you only want to look for a certain number of times, you can use “for.”
#! /usr/bin/env python3
try:
for x in range(0,4):
print("Hello world!")
except:
print("Done!")
Question
What is the value of “x” during each iteration?
You can use “if” statements to test for conditions which can include “else” clauses for what to do in exceptions.
#! /usr/bin/env python3
try:
for x in range(0,4):
if (x <3):
print("Hello world!")
else:
print("Goodbye world!")
except:
print("Done!")
Try this
You can test the modulo as a condition determine whether a value divides evenly by a divisor, e.g. “x % 2 == 0” will only be true for even numbers.
This only scratches the surface for how you might direct flow control.
Key Points
Loops can be unconditional or conditional