Lab 1, Exercise 5: Flow Control
In computer programs, flow control refers to the order in which the computer executes the instructions you've provided in the code. The Python interpreter will execute your instructions in the same order you read them: left-to-right on a single line, and top-to-bottom across the file. This is sometimes called standard flow. You don't have to do anything to tell the interpreter to render your instructions in this way. It's automatic.
But in many cases you need to interrupt the normal flow of the program. There are two ways to do this.
- A conditional statement tells the interpreter to execute only one particular block of instructions while ignoring other blocks.
- A loop tells the interpreter to execute a particular block of instructions several times.
In both cases, you must provide strict controls that tell the Python interpreter exactly what it's supposed to do. Fortunately, like most things in Python, this is very easy.
An important thing to realize at the outset is how Python uses indentation to tell the difference between code that executes according to standard flow, and code that may or may not execute, or that may execute several times. Pay close attention to how indentation is used in the following program. It's easy, and you want it to become second-nature.
Open a new IDLE programming window and save the file with the filename YourName_Lab1_PHYS495_5.py. Type the following lines:
from __future__ import division
from visual import *
"""
Filename: YourName_Lab1_PHYS495_5.py
Author: Your Name
Date: 01/17/2007
Description: Illustrations of flow control.
"""
"""
Standard flow is left-to-right and top-to-bottom,
the same way we read English text. It's automatic.
"""
x = 3.0
y = 4.0
z = 5.0
print "Standard flow:"
print "The values of x, y, z are %3.1f, %3.1f, %3.1f." %(x,y,z)
print "\n" # line break
"""
Conditional flow means one block of code will be
executed while another is ignored.
We use the IF keyword to accomplish this. Immediately
after the IF keyword is the TEST. The TEST statement
MUST END with a colon, : .
The statements that will be executed if the test is
true must be INDENTED ONCE under the if statement
"""
if x > 4.0: # i.e., if the value of x is greater than 4.0
# The statements that will be executed if the test is true
# must be indented once under the "if" statement!!!
print "Conditional flow:"
print "x is greater than 4.0" # if the test is true
print "\n" # line break
else: # i.e., if the value of x is less than or equal to 4.0
# You must indent these statements!!!
print "Conditional flow:"
print "x is less than 4.0" # if the test is false
print "\n" # line break
print "These statements are not indented,"
print "so they are not part of the IF block of code."
print "They are executed no matter what."
print "\n" # line break
"""
To test for equality, use the double-equals operator: ==
You must be very careful when comparing floating-point
numbers for equality. In many cases, this simply won't
work and we must do something different.
"""
if x == 3.0: # i.e., if the value of x is equal to 3.0
print "Conditional flow:"
print "x is equal to 3.0" # if the test is true
print "\n" # line break
else:
print "Conditional flow:"
print "x is NOT equal to 3.0" # if the test if false
print "\n" # line break
"""
We can have more than one explicit test. The code will
evaluate each test until it comes to the FIRST ONE that
evaluates as TRUE, or until it reaches the end of the
tests with the ELSE keyword.
"""
if x > 3.0: # i.e., if the value of x is greater than 3.0
print "Conditional flow:"
print "x is greater than 3.0" # if the first test is true, stop here
print "\n" # line break
elif y > 4.0: # i.e., if the value of y is greater than 4.0
print "Conditional flow:"
print "y is greater than 4.0" # if the 2nd test is true, stop here
print "\n" # line break
elif z < 10.0: # if the value of z is less than 10.0
print "Conditional flow:"
print "z is less than 10.0" # if the 3rd test is true, stop here
print "\n" # line break
else: # i.e., if NONE of the tests are true
print "Conditional flow:"
print "No true tests in this block of code!!"
print "\n" # line break
"""
A loop is a block of code that executes many times.
We create a loop with the WHILE keyword.
Immediately after the WHILE keyword is the TEST. The loop
continues to execute as long as the test is true.
"""
t = 0.0 # initial value
t_max = 20.0 # maximum value
dt = 2 # step
print "A loop:"
print "\n" # line break
while t < t_max: # i.e., while t is less than t_max
t += dt # increments the value of t by the value of dt
print "t = %3.1f, t_max = %3.1f " %(t, t_max)
"""
Create a while loop that runs from t = 0.0 to t_max = 25.0,
with a step size (dt) of 0.5.
At each iteration of the loop, your program should print the
value of t and the square root of t, both displayed to 4 decimal
places.
You will need to reinitialize the values of t, t_max and dt before
the loop!!
Don't forget to include comment statements to explain what you're
doing!
"""
Hit the F5 key to execute your program. Make sure the output makes sense. It should look something like this:
Turn in all your programs to the Digital Dropbox on Blackboard.
Next week: We launch our first rocket!
Previous: Exercise 4
Go back to the Lab 1 main page.