VPython Loops

From Physics Book
Revision as of 22:43, 26 November 2016 by Ncastro9 (talk | contribs)
Jump to navigation Jump to search

Claimed by Nicolas Castro (Fall 2016)

What loops are and how to use them in a VPython Program

The Main Idea

In programming, loops exist to execute a singular or series of statements for a specified number of times. This simplifies executing any function multiple times. Depending on the type of loop, the functions will know when to be carried out and how many time/for how long it will be carried out.

For Loop

For loops are based on a specified list and can repeat a function a specific number of times.

Examples

For loops can be used in conjunction with a list of items or numbers.

solarsystem = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
for planet in solarsystem:
    print(planet) 

In the example of above a list will be printed as follows:


For loops can also be used to complete a function for a certain number of times given as a range.

for x in range(0,3):
    print "I love physics!" 

This will print the print the give phrase 3 times.

While Loop

While loops are used to repeat a function until a certain value or criteria is met, which is generally less restrictive than a for loop.

Examples

While loops are very useful in physics for representing time intervals. For example, if you wanted to express that a object was moving over a certain time period you could represent it as such:

#initial values
ball = sphere(pos=vector(0, 0, 0), radius=1)
time = 0
velocity = 5
#calculations
while time < 100
     time = time+10
     pos = velocity*time 

This loop starts with the initial values of position = 0 meters, time = 0 seconds, and velocity = 5 m/s. The loop will run until the time value reaches 100 seconds. Inside the loop, time is first updated so that every iteration of the loop increases the time value (i.e. the first run of the loop time becomes 10 seconds, the second run time becomes 20 seconds, and so on). Next the position is updated using a physics formula: change in distance = velocity * time.

While loops can also be used to create objects in a pattern. For example, if you wanted to create series of spheres in a line you could use the following code:

#initial values
distance = 0
#calculations
while distance < 100:
    distance = distance + 10
    ball = sphere(pos=vec(distance,0,0) , radius=1)
 

This loop will create a ball of the same radius in a line along the x axis every 10 meters. It's possible to alter distances along the y and z axis the same why simply by creating a variable for the y or z part of the vector.

While loops can also be used to create objects in a circular path!

#initial values
theta = 0
#calculations
while theta < 2*pi:
    theta= theta + pi/6
    location = vector(cos(theta),sin(theta),0)
    ball = sphere(pos=(location), radius=0.1) 

This code creates a series of 12 spheres in a circle by changing theta each time the loop is iterated. Changing the the increment by which theta is increased (in this case pi/6) you can change the number of spheres that are formed.


History

The idea of loops predate computer programming, but the first instance of loops being used in this application was by Ada Lovelace to calculate Bernoulli numbers which was described in 1842.

See also

External links

1. More on For Loops

2. More on While Loops

3. Loops and Sequences