Check your understanding of the previous reading: the parts of a while loop, when a while loop stops, and how while loops compare to for loops.

Question 1

Identify each part of the while loop shown:

The line `while command != "end":` above an indented `pass`, with five numbered labels: 1 points at `while`, 2 at `command != "end"`, 3 at the colon, 4 at the start of the second line, left of `pass`, and 5 at `pass`.

LabelPoints atPart
1 while
2 command != "end"
3 :
4 the start of the second line, left of pass
5 pass

Question 2

Syntactically, while loops look just like for loops. They both have a keyword, a colon, and a list to iterate over.

Select one answer, then check.

  • Not quite. A while loop has no list to iterate over: after the keyword while comes a condition, as in an if statement.
  • Correct. Both start with a keyword and end their first line with a colon, but a while loop has a condition where a for loop has a list.

Question 3

A While loop will always execute at least once.

Select one answer, then check.

  • Not quite. The condition is checked before the body runs the first time; if it is false then, the program skips the body entirely.
  • Correct. If the condition is false the first time it is checked, the body never runs.

Question 4

The program below will NOT loop forever.

count = 10
while count >= 0:
    count = count - 1
print(count)

Select one answer, then check.

  • Correct. count goes down by one on every pass, so it reaches -1 and the condition count >= 0 becomes false.
  • Not quite. count goes down by one on every pass, so once it reaches -1 the condition count >= 0 is false and the loop ends.

Question 5

The program below will NOT loop forever.

count = 0
while count > 0:
    count = count + 1
print(count)

Select one answer, then check.

  • Correct. count starts at 0, so count > 0 is false the first time it is checked: the body never runs, and the program prints 0.
  • Not quite. The body would increase count forever, but it never runs: count > 0 is already false when count is 0.

Question 6

The program below will NOT loop forever.

alpha = 'dog'
while alpha == 'dog':
    alpha = 'cat'
    print(alpha)
    alpha = 'dog'
print(alpha)

Select one answer, then check.

  • Not quite. alpha is 'cat' only in the middle of the body; by the end of every pass it is 'dog' again, so the condition is true every time it is checked.
  • Correct. The condition is only checked at the start of each pass, and by then alpha is always back to 'dog', so the loop never ends.

Question 7

The program below will NOT loop forever.

while True:
    print("Loop again")

Select one answer, then check.

  • Not quite. The condition is the value True, which never becomes false, and nothing in the body stops the loop.
  • Correct. The condition is the value True, which never changes, so the loop prints Loop again forever.

Question 8

while loops are more common in Python than for loops because they are safer.

Select one answer, then check.

  • Not quite. for loops are the most common form of iteration in Python, and they are the safer one: they end when the list runs out, while a while loop can easily loop forever by accident.
  • Correct. It is the other way around: for loops are more common, and safer, because they end when the list runs out.

Question 9

A while loop will execute once for each expression in the conditional.

Select one answer, then check.

  • Not quite. The number of passes has nothing to do with the expressions in the condition: the body repeats for as long as the condition is true.
  • Correct. A while loop repeats its body for as long as the condition is true, however many expressions the condition has.

Question 10

Which of the following have a body?

Select every answer that applies, then check.

  • Correct. The indented statements under the while line are its body.
  • Correct. The indented statements under the for line are its body, which runs once for each element.
  • Correct. The indented statements under the def line are the body of the function.
  • No body. A call runs the body of a function that was defined elsewhere; the call itself has no indented block.
  • Correct. The indented statements under the if line are its body.
  • No body. Looking up a value is an expression, not a block of indented statements.