Repeating Code Using While Loops

1. Objectives

In Chapter 1 of Matter & Interactions 4e we performed iterative calculations of motion by hand, usually taking only two or three steps. To predict motion over a longer time interval, we can instruct a computer to do the calculations for us.  A VPython structure called a while loop which allows commands to be repeated while a condition is True is useful to for this kind of task.

Before doing this activity you should have read Section 1.11 of the Matter & Interactions textbook, which gives an overview of this procedure.

After completing this activity you should be able to:

  • Understand the syntax and function of a while loop.
  • Write a while loop of specific duration.

2. While Loops

To instruct a computer to repeat a particular set of operations many times, we use a structure called a loop. A while loop is a type of loop that will continue to repeat a set of instructions as long as a condition is True as depicted in the adjacent flowchart. We will often use a loop to update the velocity and position of an object. We will include calculations of the new velocity and new position inside the body of the while loop and we will continue to execute the loop as long as some condition is True. For example, if we are computing the path of a ball that is thrown we might choose to continue the loop as long as the ball is above ground level.

The following video describes how to use a while loop in vPython. This video was produced in 2010 so it uses an older version of Python as well as a completely different programming environment than we are using with GlowScript. However, the description of how to use a while loop is still accurate for our purposes. Watch this video and notice in particular how

  • the while statement must end with a colon (:), and
  • the body of the loop is indicated by indenting using a tab character.

3. Predicting the Outcome of a While Loop

  • Consider the code shown below which contains a while loop.
  • Try to predict what will happen when this code is run in GlowScript.
  • Copy the code into a new GlowScript program and give it a try.
offset = vector(0,-3,0) # Define a constant offset
position_now = vector(0,0,0) # Define an initial position
n=0
while (n < 5):
    sphere(pos=position_now, color=color.red)
    position_now = position_now + offset
    n=n+1
  • Explain what you observe and make sure you understand why the program behaves in the manner you observe.

  • Now consider the following program carefully. What will its output look like?
  • Draw your prediction before running the program.
ball = sphere(pos=vector(-5,0,0), radius=0.5, color=color.green)
block = box(pos=vector(-8,0,0), color=color.yellow)
velocity = vector(0.4, 0.6, 0)
delta_t = 0.1
t=0
while (t < 12):
    rate(100)
    ball.pos = ball.pos + velocity * delta_t
    t = t + delta_t
  • Run the program. Was your prediction correct?
  • Make sure you understand what the statement rate(100) does.  Try the code both with the statement and without it (you can comment it out).  This command slows the execution of the loop down to a maximum of 100 iterations per second.
  • Make sure you understand why this program moves the sphere, while the previous program created many spheres.