Iteration 1A

[these notes have been updated, please see the update]
Think of how your brain counts a sequence of numbers-- if someone asks you to add up the first five numbers, how would you do it? You use some memory cells-- what would you call those memory cells if you had to name them?



Iteration means to repeat a process. Here's the wikipedia definition:
 
“Iteration is the repetition of a process, typically within a computer program”

In Python programming, the while statement let's you tell the computer to run a command more than once:

    while <someCondition>:
            <command1>
      
     <command2>
 
The above would be interpreted as 'while someCondition is true, keep running command1 and command2.'

Note that the <someCondition> is not valid Python syntax-- the <someCondition> means you can put some Python boolean condition there. A boolean condition is a question that evaluates to true or false.

Note that you must put a colon after the condition, and you MUST indent the commands that belong within the while loop. The code below:
 
    while <someCondition>:
        <command1>
    <command2>
 
will run command1 0 or more times, dependent on someCondition, and then execute command2 exactly once.
 
Let's work on some simple examples. Suppose we want to print the numbers 0 through 4. Without iteration, we could type the following in a Python program:

 print 0    
 print 1   
 print 2    
 print 3
 print 4
 
Obviously, this can get laborious-- just think if we wanted to print the first thousand numbers. Iteration-- a while statement-- can help. We set up a counter variable to tell us how many times we've done something. In this case, we start the variable at 0, and add one to it until it gets to 5.  We can do something like the following:

 
    number=0
    while number<5:
        print number # this statement and the one below it is executed many times, within the loop
        number=number+1
 
In this sample,

Note that you must put a colon after the condition (number<5), and you must indent the commands that belong within the while loop.




The above code will execute the print command and the 'number=number+1' statement over and over while the condition 'number<5' remains true.

What will happen if the last line is not indented?
 
number = 0
while number<5:
    print number  
          number = number +1

Note that you can stop a program, stuck in a never-ending loop, by pressing control-C. And don't worry, the computer won't get bored or start smoking from overwork.

Before we move on, let's make another mistake to see what will happen. What will happen for the following code:
   
    while number<5:

        print number
        number=number+1


The code below is a template for iterating through some commands n times (n=5 for this sample).

IMPORTANT PROGRAMMING TEMPLATE

 i=0   # initialize your loop variable
 while i<5:
      print i  # in general, do what you want to do 5 times here
      i=i+1   # increment your loop variable


In the template, 'i' is used instead of 'number' as the counter variable. You can use any name for the counter. 'i' is often used and is short for 'index'.

Memorize the template above—you’ll write very similar code over and over, changing the '5' to the number of times you want to do something, and the 'print i' to a command (or commands) that you want to repeat.

Key Things to Remember

  • Remember to initialize the counter variable (often to zero).
  • Remember to increment the counter variable within the loop

In-Class Problems

1. Try the code samples in the notes above. Use a text editor to enter them in a file loop.py and run them. Remember, hold down the control key and hit C to stop a ‘never-ending’ loop.
 
2. Create a program backloop.py that prints the numbers from 10 down to 1 (reverse order).
 
3. Create a program everythird.py that prints every third number from 1 to 100 (1, 4, 7, etc.).

4. Write a program firstfive.py
that adds up the first 5 numbers (1-5). Use a while loop. The program should print

the total is 15

Hint: you'll need a counter variable i and a variable total that records the running total of your additions.

 
5. Programmer Milestone 1: Write a program total.py that adds up the first n numbers using a loop, where n is a number input by the end-user.
Begin by saving (Save As) firstfive.py into total.py. You'll need to introduce another variable, n.

Here's a sample run of the program, with the user's input in bold:

    please enter a number n: 7
    The total is: 28

6. Interest Rate: Write a program that computes how much money a person will have after n years, given an initial principal, interest rate, and number of years n, all input by the end-user.

When you complete these problems, copy-paste the code of each program onto your Google Sites portfolio, providing a short explanation for how each program works. Also, attach the actual .py files into your portfolio.

Recent site activity