From CS 61A Wiki
Revision as of 04:40, 7 June 2014 by Cem (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Jump to: navigation, search

Iteration is the process of repeatedly executing a part of the code that is engulfed inside an iterative statement block. Quite frequently we feel the need to execute a part of the code multiple times and computers are very good at repeating identical or similar tasks thus only through repeated execution of statements do we unlock the full potential of computers. Also when writing code, instead of unnecessarily repeating ourselves by writing the same lines of code over and over again in multiple parts of our program, we can simply put that part of the code inside a loop. Almost every programming languages (including Python) offers multiple ways to create a loop to iterate over a part of a code.

The while statement

The simplest form of iteration is by using a while statement block. While statement contains an expression header and a suite of expressions.

while <expression>:


To execute a while clause:

  1. Execute the header expression.
  2. If it is True execute the suite of expressions then return to step 1.

This where you should be careful. The entire suite of expressions is evaluated before we return and check the header expression again. A while statement executes its suite until the header expression evaluates to a False value. Then it stops and computer continues executing the remaining parts of the program after the while statement block. If a while statement doesn't stop it is called an infinite loop. In the Python interpreter press <Control>-C to terminate the execution.

Example: First 10 fibonacci numbers

Lets assume that one day Rohin wanted to challenge Andrew; so he asks Andrew to find a way to write out first 10 fibonacci numbers. Now fibonacci sequence is a special mathematical sequence where every other number is created by adding previous two numbers. Andrew knows that fibonacci sequence starts with 1 1 2 however lets assume that he doesn't know the rest. So he creates this logic. If he starts with first two values of fibonacci sequence he can add those two numbers to create the third number and then add the 3rd and 2nd number to create the 4th and then 4th and 3rd to create the 5th and so forth. He realizes that Rohin gave Andrew an iterative process and he can use his while statement. He writes:

i = 10
previous = 0
current = 1
while i > 0:
    previous, current = current, previous + current
    i = i - 1    print(previous)

Remember in the assignment previous, current = current, previous + current the key thing is that before any assignment is made to previous or current first we evaluate the right part and then make the assignment. In here first thing on the right is current, and then we add previous and current and then make previous the current and finally make current the new added value. Another key thing is emphasized in the highlighted part where we decrement the value of i to make the while loop stop when it executes 10 times. Andrew gives his result to Rohin and renders him speechless.

The for statement