While-Loops

Here is a while loop:
      n = 1
      while n <= 5:
           print n
           n = n + 1
      print 'done!'
    
It yields the following output:
      1
      2
      3
      4
      5
      done!
    
How does this work? A while statement is followed by an indented block of code. That block of code is repeated, as long as the while-condition is True.

Step-by-step, the following happens:

  1. n is 1
  2. 1 <= 5 is True, so
  3. we print 1, and set n to 2.
  4. 2 <= 5 is True, so
  5. we print 2, and set n to 3.
  6. 3 <= 5 is True, so
  7. we print 3, and set n to 4.
  8. 4 <= 5 is True, so
  9. we print 4, and set n to 5.
  10. 5 <= 5 is True, so
  11. we print 5, and set n to 6.
  12. 6 <= 5 is False, so
  13. we skip the block of code, and print done!
Here is another example, which adds numbers from 1 to 10:
        n = 1
        sum = 0
        while n < 11:
            sum += n
            n += 1
        print sum
      
(notice that I've used a shorthand: n += 1 is equivalent to n = n + 1)

The while-condition checks that n is less than 11, so n will go all the way up to 10, then the loop will exit.

It's also possible to have one loop inside another:

        n = 1
        while n <= 4:
            print 'n =', n
            x = 1
            while x <= 3:
                print '.... x =', x
                x += 1
            n += 1
      
In the outer loop n runs from 1 through 4, and in the inner loop x runs from 1 through 3, yielding this output:
        n = 1
        .... x = 1
        .... x = 2
        .... x = 3
        n = 2
        .... x = 1
        .... x = 2
        .... x = 3
        n = 3
        .... x = 1
        .... x = 2
        .... x = 3
        n = 4
        .... x = 1
        .... x = 2
        .... x = 3
      

For-loops

We often write loops where some variable runs from 1 up to some value. That's for-loops were invented. Here is an example:
        for n in range(5):
            print n
      
which yields this output:
        1
        2
        3
        4
      
The function range(5) produces this list of values: (1 2 3 4). This is a list of ints, starting at 1, and stopping at one less than 5.

The statement for n in list works as follows: variable n gets each of the values in the list. For each value, the indented block of code is executed.

Of course, you can nest one for loop inside another:

        for i in range(4):
            print 'i =', i
            for j in range(3):
                print '... j =', j
                for k in range(3):
                    print '------ k =', k
        
This produces this output:
          i = 1
          ... j = 1
          ------ k = 1
          ------ k = 2
          ... j = 2
          ------ k = 1
          ------ k = 2
          i = 2
          ... j = 1
          ------ k = 1
          ------ k = 2
          ... j = 2
          ------ k = 1
          ------ k = 2
          i = 3
          ... j = 1
          ------ k = 1
          ------ k = 2
          ... j = 2
          ------ k = 1
          ------ k = 2
        

Breaking a loop

Sometimes you don't want the loop to run through all its steps. You may want to stop early. Use the break statement. For example, say we're looking for all multiples of 17, between 100 and 200:
          for n in range(100, 201):
              if n % 17 == 0:
                  print n, 'is a multiple of 17'
        
This will output:
          102 is a multiple of 17
          119 is a multiple of 17
          136 is a multiple of 17
          153 is a multiple of 17
          170 is a multiple of 17
          187 is a multiple of 17
        
But now we decide to stop after the first multiple of 17:
          for n in range(100, 201):
              if n % 17 == 0:
                  print n, 'is a multiple of 17'
                  break
        
This will output:
          102 is a multiple of 17
        
The break statement immediately stops the current loop, whether it is a while loop or a for loop.

The break statement interrupts the nearest enclosing loop. If we have one loop inside another, a break inside the inner loop won't affect the outer loop. Thus, we could consider the numbers from 100 to 200, and find the first multiple of 2, 3, 5, 7, 11, and 13 among them. To this, we would take the loop above, and put inside another loop that checks all these factors.

          for factor in (2, 3, 5, 7, 11, 13):
              for n in range(100, 201):
                  if n % factor == 0:
                      print n, 'is a multiple of', factor
                      break
        
which will output:
          100 is a multiple of 2
          102 is a multiple of 3
          100 is a multiple of 5
          105 is a multiple of 7
          110 is a multiple of 11
          104 is a multiple of 13