Skip to content
ExamSpine

Lesson 3.1

Loop until a stop value

Part 3: RepetitionReading, about 4 min3 exercises in this lesson, in Python2 exercises in this lesson, in Java

Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.

Loop until a stop value

By the end of this you will be able to write a loop that keeps going until the user types a stop value, and trace how many times it runs.

Every program so far runs its lines once. A while loop repeats a block for as long as a condition is true. When the condition becomes false, the loop stops and the program moves on.

A while loop

line = input()
while line != "stop":
    print("You typed: " + line)
    line = input()
print("Done")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    String line = scanner.nextLine();
    while (!line.equals("stop")) {
        System.out.println(
            "You typed: " + line);
        line = scanner.nextLine();
    }
    System.out.println("Done");
}

If the user types hello, then world, then stop, it prints You typed: hello, then You typed: world, then Done. The word stop is never printed because the condition is checked before the block runs.

How to read it

  1. The condition is checked.
  2. If true, the block runs, then go back to step 1.
  3. If false, the block is skipped and the program continues after it.

The condition is checked every time, including the first. If the very first input is stop, the block never runs and Done prints straight away.

The read-check-read pattern

Notice that the program reads a line in two places: once before the loop (the first read) and once at the end of the loop body (every later read). This is the standard pattern for "read until a stop value":

  1. Read once before the loop.
  2. Check the value in the while condition.
  3. Use the value inside the loop.
  4. Read again at the end of the body.

If you forget step 4, the value never changes, so once the loop starts it runs forever. The next lesson covers that mistake.

Now try it yourself on the questions below.

Exercise 3.1.1 · Trace itTrace: while countdown

Complete the trace table for this Python code. [2]

Line 1: n = 3
Line 2: while n > 0:
Line 3, indented 1 level: print(n)
Line 4, indented 1 level: n = n - 1
Line 5: print("go")

Fill in one row each time a line changes n, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rownoutput
1
2
3
4
5
6
7
8

Exercise 3.1.23.1.1 · Write itCount until stop

Write a program that reads words one at a time and counts how many words were typed before the word stop.

  1. Read one word per line.
  2. When the word is exactly stop, stop reading.
  3. Print Count: followed by the number of words that came before stop.
  4. The word stop itself is not counted.
  5. If the very first word is stop, the count is 0.
  6. The comparison is case sensitive: Stop is not the same as stop.

Your program is run several times. Only what it prints is checked.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • two words then stop (rules 3 and 4)

    You type

    1. typed: hello
    2. typed: world
    3. typed: stop

    It prints

    Count: 2
  • stop immediately, count is 0 (rule 5)

    You type

    1. typed: stop

    It prints

    Count: 0
  • one word then stop (rule 3)

    You type

    1. typed: one
    2. typed: stop

    It prints

    Count: 1
solution.pySolution.java

Exercise 3.1.33.1.2 · Write itSum until zero

Write a program that reads whole numbers one at a time and prints their sum when it reads 0.

  1. Read one number per line.
  2. Keep a running total, starting at 0.
  3. Add each number to the total.
  4. When the number is 0, stop reading and print Sum: followed by the total.
  5. The stop value 0 is not added to the total.
  6. If the very first number is 0, the total is 0.

All numbers are whole numbers. Your program is run several times. Only what it prints is checked.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 7 times: the 3 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked.

  • two positive numbers then stop (rules 3 and 4)

    You type

    1. typed: 5
    2. typed: 3
    3. typed: 0

    It prints

    Sum: 8
  • one number then stop (rule 3)

    You type

    1. typed: 10
    2. typed: 0

    It prints

    Sum: 10
  • stop immediately, sum is 0 (rule 6)

    You type

    1. typed: 0

    It prints

    Sum: 0
solution.pySolution.java