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.
Identify each part of the while loop shown:

| Label | Points at | Part |
|---|---|---|
| 1 | while | |
| 2 | command != "end" | |
| 3 | : | |
| 4 | the start of the second line, left of pass |
|
| 5 | pass |
Read the first line from left to right: the keyword, the condition it checks, and the character that ends the line. Labels 4 and 5 are about the second line: how it is placed, and what it holds.
1 While keyword (while), 2 Conditional (command != "end"), 3 Colon, 4 Indentation (the spaces before pass), 5 Body (pass).
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.
while loop has no list to iterate over: after the keyword while comes a condition, as in an if statement.while loop has a condition where a for loop has a list.A While loop will always execute at least once.
Select one answer, then check.
The program below will NOT loop forever.
count = 10
while count >= 0:
count = count - 1
print(count)
Select one answer, then check.
count goes down by one on every pass, so it reaches -1 and the condition count >= 0 becomes false.count goes down by one on every pass, so once it reaches -1 the condition count >= 0 is false and the loop ends.The program below will NOT loop forever.
count = 0
while count > 0:
count = count + 1
print(count)
Select one answer, then check.
count starts at 0, so count > 0 is false the first time it is checked: the body never runs, and the program prints 0.count forever, but it never runs: count > 0 is already false when count is 0.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.
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.alpha is always back to 'dog', so the loop never ends.The program below will NOT loop forever.
while True:
print("Loop again")
Select one answer, then check.
True, which never becomes false, and nothing in the body stops the loop.True, which never changes, so the loop prints Loop again forever.while loops are more common in Python than for loops because they are safer.
Select one answer, then check.
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.for loops are more common, and safer, because they end when the list runs out.A while loop will execute once for each expression in the conditional.
Select one answer, then check.
while loop repeats its body for as long as the condition is true, however many expressions the condition has.Which of the following have a body?
Select every answer that applies, then check.
while line are its body.for line are its body, which runs once for each element.def line are the body of the function.if line are its body.