Loops - For and While

Learning Objectives:

Example For Loop

We have been using a for loop to repeat the body of a loop a known number of times. The body of a loop is all the statements that follow the for statement and are indented to the right of it. Be sure to indent 4 spaces to indicate the statements that are part of the body of the loop.

Before you step through the code in for counter below, try to predict the last value it will print and then answer the question below.

    csp-8-1-1: What is the last value that will be printed when the code below executes?
  • 9
  • A range goes from a starting point to one less than the ending point. If we want to count to 10, use range(1,11).
  • 10
  • The range doesn't include the end value. It stops at one before the last value.
  • 11
  • In fact, it doesn't even get to 10! Try it.

If we want to print out a count that starts at 1 and increases by 1 each time, we can do easily do that with a for loop. Use the Forward button to see how counter takes on a new value each time through the loop.

(for_counter)

Introducing the While Loop

Another way to repeat statements is the while loop. It will repeat the body of loop as long as a logical expression is true. The body of the loop is the statement or statements that are indented 4 or more spaces to the right of the while statement. A logical expression is one that is either true or false, like x > 3.

While loops are typically used when you don’t know how many times to execute the loop. For example, when you play a game you will repeat the game while there isn’t a winner. If you ever played musical chairs you walked around the circle of chairs while the music was playing.

The code below will loop as long as the number that you enter isn’t negative. It will add each non negative number to the current sum. It will print out the sum and average of all of the numbers you have entered.

    csp-8-1-2: How many lines are in the body of the while loop in while_input above?
  • 3
  • All the statements that are indented 4 spaces to the right of the while are part of the body of the loop.
  • 4
  • There are four statements that are indented 4 spaces to the right of the while statement, so there are four statements in the body of this loop.
  • 5
  • Is the print(message) line indented 4 spaces to the right of the while? If not it is not part of the body of the loop.
  • 6
  • While line 11 is indented this is just to allow the print statement to take up more than one line. The print statement is not indented so the body of the loop contains just 4 lines.
    csp-8-1-3: What happens if you enter a negative number as the first input to the code above?
  • It prints the sum is 0 and the average is 0.
  • Do you see code to do this in the program?
  • It prints a message that it can't divide by 0.
  • This might be nice, but is that what happens?
  • There is an error.
  • You will get a ZeroDivisionError since you can't divide by zero.
Next Section - Infinite Loops