Loops

Within any programming language there are control structures known as loops. Loops are used when you want to repeatedly execute the same block of code. The most common use of loops is when you have a list of items you want to search through or sort. There are two types of loops that are used, the whlie loop and the for loop. The while loop is generally used when you have a boolean condition and you want to reiterate until that condition changes. The for loop is commonly used when you know how many times you want to reiterate. The following code examples demonstrate how the different types of loops work.

The For Loop
The for loop is used when you want to execute some code a certain number of times. Each for loop has a range which indicates the number of times the code within the loop will be executed: for i in range(0,11): print i This loop says start at 0 and loop up to but not including the value 11 and print all the values. Range is exclusive because it does not include this 11 value. Therefore, this loop will print all the values from 0 to 10. When you start from 0, you may also think about the second value as how often the loop executes. This loop executes 11 times because 0 through 10 is eleven numbers. Programmers often start from the value 0 rather than 1 when counting, and for-loops are no exception.

The variable i in the previous example is also known as the counter, because it keeps track of the count of the loop. The following examples demonstrate how to use the for loop:

Summation Example

#Sum the numbers from 1 to 10 sum = 0 for i in range(1,11): sum = sum + i   print sum    #The result is 55

Fibonnaci Numbers Using For Loops a = 0 b = 1 for i in range(0,11):   #Do up to the 11th fibonacci number print a       a,b = b,a+b #assigns a to b, and b to a+b

Nested For Loops

The first loop in the following example will iterate 10 times. For each of these iterations the second loop will iterate 10 times and each time the variable a is incremented by 1. Therefore a gets incremented 100 times and will equal 100 at the end: a = 0 for x in range(0,10): for x in range(0,10): a += 1 print a  #prints 100

The Foreach Loop
The for loop can even be used directly on data structures (lists, tuples) in order to loop through each element within them. When used in this context, the for loop is called a foreach loop because it loops "for each" element in the list. The foreach syntax is exactly the same as the regular for loop, except the list that is iterated over is not just numbers, but actual objects: for animal in ["Dog", "Cat", "Fish", "Bird"]: print "At the pet shop, I saw a " + animal This prints out: At the pet shop, I saw a Dog At the pet shop, I saw a Cat At the pet shop, I saw a Fish At the pet shop, I saw a Bird

The While Loop
The while loop is similar to a for loop except that it will iterate until a condition statement is False rather than using a counter. The format for every while loop is as follows: while (some conditions): # do work

Suppose you have a list of letters and you want to find the position of a certain letter. The following code searches for the letter D and then prints out the iteration which found it:

found = 0 count = 0; list = ["A","B","C","D","E"] # Loop while found is still false and count is not higher than list size while found == False && count < len(list): print list[count] if list[count] == "D": print "D found on iteration", count found = True; # This will cause the loop to stop else: count += 1  #increment count to look at the next letter in list The previous while loop was just an example to help show how they worked. You may have noticed that iterating through a list that way would probably be even easier using the aforementioned for loop to iterate on the list using the in keyword. Because while loops do not depend directly on a set amount of items, they are often used when the condition can be changed at different points within the loop. A more real-world example would be waiting for a message. Here, let's wait for a message using an arbitrary function we'll call receive_msg: is_received = False while (!is_received): is_received = received_msg
 * 1) ! is the "Not" operator.
 * 2) Therefore, this will loop while is_received is False