Loops

The loop construct in Python allows you to repeat a body of code several times. There are two types of loops - definite loops and indefinite loops. You use a definite loop when you know a priori how many times you will be executing the body of the loop. You use key word for to begin such a loop. You use an indefinite loop when you do not know a priori how many times you will be executing the body of the loop. The loop body may be executed zero or more times. You use the key word while to begin indefinite loops.

To write an efficient loop structure you must ask yourself 3 questions:

While Loop

Syntactically the simplest loop construct is the while loop.

  while (cond):
    ...
    loop_body
    ...
The term cond is a boolean expression. The body of the loop gets executed if the value of cond is True and terminates when cond is False. If cond is False to start with then the body of the loop does not get executed even once.

For Loop

A definite loop starts with for and then the sequence or range is given. A definite loop uses a loop counter. By convention, we use i as the loop counter but any variable could be used in its place. Here are several examples of the use of the for loop.

Write a loop that executes the body 10 times.

for i in range (10):
  ...
  loop_body
  ...
The loop counter i goes through the values 0 through 9 as the loop iterates.

We can also specify the beginning and ending value of i. Let us say we wanted i to start at 3 and end at 14. Then the loop could be written as:

for i in range (3, 15):
  ...
  loop_body
  ...
Note that we used 15 instead of 14 as the ending value since the ending value is not inclusive. The loop counter i will take all the values between 3 and 14 both inclusive in the above piece of code.

We can also specify the step size for the loop counter.

for i in range (1, 10, 2):
  ...
  loop_body
  ...
Here i will take all the odd values from 1 to 9 inclusive.

If the step size is not uniform and cannot be expressed by a simple mathematical formula then we can also enumerate all the values that the loop counter i will take. The set of values that i will take is called a sequence.

for i in [7, 4, 18, 12]:
  ...
  loop_body
  ...
Note that stating the sequence only works for a small set of numbers but becomes cumbersome if you have to write lots of numbers explicitly in your program.

Break and Continue

There are times when you may want to stop executing the body of the loop even before the iteration has ended. This may happen if you have already found the answer that you were looking for and you do not want to either waste computing time or introduce extraneous values in your computation. In such situations use the keyword break. This will cause execution of the loop to stop or break at that point.

There are other occasions where you may not want to execute the remaining code in the body of the loop but start the next iteration. The keyword to use in this situation is continue. Using the continue statement will not stop the execution of the loop but start the next iteration.