Indefinite iteration repeats code while a condition stays true. Use it when you do not know in advance how many times you will loop — for example until the user types stop, or until a running total of scores is high enough.
That is different from definite iteration with for and range, where the number of passes is fixed before the loop starts.
When should you use indefinite iteration instead of a for loop with range?
{"answers": {"A": "When you do not know in advance how many times you will repeat", "B": "When you need exactly 10 numbered passes", "C": "When you only want to print one line", "D": "When you must never stop the program on purpose forever"}, "question_text": "When should you use indefinite iteration instead of a for loop with range?", "correct_answer": "A"}
while True — an infinite loop
The simplest while form is while True. The condition is always true, so the body keeps running. With nothing to stop it, this is an infinite loop — the program never moves on.
Open this idea carefully: students should see why a forever loop needs a clear exit later. On its own, this prints forever (stop the program manually in the IDE if you run it):
while True:
print("still going")
still going
still going
still going
... (keeps going)
What happens if you run while True: print("hi") with no break?
{"answers": {"A": "It is an infinite loop and keeps printing", "B": "It prints once and stops", "C": "Python rejects while True", "D": "It waits for the user to type hi"}, "question_text": "What happens if you run while True: print(\"hi\") with no break?", "correct_answer": "A"}
Stopping with input and break
To make while True useful, add input and break. The loop can still run forever in principle, but when the user types the stopping word, break leaves immediately and the program continues after the loop.
This is the usual first repair after seeing an infinite loop: keep the forever structure, then choose a clear exit from inside the body.
while True:
reply = input("Type quit to stop: ")
if reply == "quit":
break
print("You typed:", reply)
print("Done")
Type quit to stop: hi
You typed: hi
Type quit to stop: quit
Done
In this pattern, what does break do?
while True: reply = input("> ") if reply == "quit": break
{"answers": {"A": "Leaves the loop immediately when the user types quit", "B": "Skips one input and stays in the loop forever", "C": "Restarts the whole program", "D": "Deletes the reply variable"}, "question_text": "In this pattern, what does break do?\n\nwhile True:\n reply = input(\"> \")\n if reply == \"quit\":\n break", "correct_answer": "A"}
continue to skip bad input
continue skips the rest of the current pass and starts the next one. The loop itself does not end — only this iteration is cut short. That is useful when one input is useless (for example a blank line) but later inputs should still be read.
Here blank replies are ignored; quit still uses break to finish:
while True:
reply = input("Enter a colour (or quit): ")
if reply == "":
continue
if reply == "quit":
break
print("Colour:", reply)
Enter a colour (or quit):
Enter a colour (or quit): blue
Colour: blue
Enter a colour (or quit): quit
What does continue do inside a loop?
{"answers": {"A": "Skips the rest of this pass and starts the next iteration", "B": "Ends the loop completely", "C": "Pauses the program until Enter is pressed", "D": "Converts the condition to False"}, "question_text": "What does continue do inside a loop?", "correct_answer": "A"}
Running total of scores
A clear indefinite example is a running total of scores until a target is reached. You do not know how many scores will be typed — only that you stop when the total is high enough. That stopping rule is a changing state, not a fixed trip through range.
Each pass reads another score and adds it; the while condition checks the total:
total = 0
while total < 20:
score = int(input("Next score: "))
total = total + score
print("Total so far:", total)
print("Reached the target")
Next score: 7
Total so far: 7
Next score: 8
Total so far: 15
Next score: 6
Total so far: 21
Reached the target
Why is a running total of scores a good while-loop example?
{"answers": {"A": "You stop when a condition on the total is met, not after a fixed number of range passes", "B": "Scores can only be processed with range(10)", "C": "while loops cannot add numbers", "D": "It always runs exactly four times"}, "question_text": "Why is a running total of scores a good while-loop example?", "correct_answer": "A"}
Putting it together
Putting it together: start from while True (which alone never stops), then add break for a clean exit and continue to skip bad input. Prefer a real condition — like a running total of scores — when the loop should stop because of a changing value, not because range finished.
total = 0
while True:
score = int(input("Score (0 to finish): "))
if score == 0:
break
if score < 0:
continue
total = total + score
print("Final total:", total)
Score (0 to finish): 5
Score (0 to finish): -1
Score (0 to finish): 3
Score (0 to finish): 0
Final total: 8
What does continue do inside a loop?
{"answers": {"A": "Skips the rest of the current iteration and starts the next one", "B": "Ends the loop and the whole program", "C": "Deletes the loop variable", "D": "Turns the while loop into a for loop"}, "question_text": "What does continue do inside a loop?", "correct_answer": "A"}
Which keyword starts a condition-controlled loop in Python?
{"answers": {"A": "while", "B": "for", "C": "loop", "D": "repeat"}, "question_text": "Which keyword starts a condition-controlled loop in Python?", "correct_answer": "A"}
How does indefinite iteration differ from definite iteration?
{"answers": {"A": "Indefinite depends on a condition; definite has a known count in advance", "B": "Indefinite always means the loop never stops", "C": "Definite loops cannot print output", "D": "Indefinite loops cannot use variables"}, "question_text": "How does indefinite iteration differ from definite iteration?", "correct_answer": "A"}
If a while condition is false at the start, what happens?
{"answers": {"A": "The body does not run at all", "B": "The body runs once then stops", "C": "Python raises an error", "D": "The loop runs forever"}, "question_text": "If a while condition is false at the start, what happens?", "correct_answer": "A"}
What is the output?
i = 0 while True: print(i) if i == 2: break i = i + 1
{"answers": {"A": "0 1 2", "B": "0 1", "C": "1 2 3", "D": "0 1 2 3"}, "question_text": "What is the output?\n\ni = 0\nwhile True:\n print(i)\n if i == 2:\n break\n i = i + 1", "correct_answer": "A"}
What is the output?
i = 0 while i < 4: i = i + 1 if i == 2: continue print(i)
{"answers": {"A": "1 3 4", "B": "1 2 3 4", "C": "2 3 4", "D": "0 1 3"}, "question_text": "What is the output?\n\ni = 0\nwhile i < 4:\n i = i + 1\n if i == 2:\n continue\n print(i)", "correct_answer": "A"}
Which statement best describes an infinite loop?
{"answers": {"A": "A failure mode where the condition never becomes false", "B": "Another name for every while loop", "C": "A for loop that uses range()", "D": "A loop that always runs exactly once"}, "question_text": "Which statement best describes an infinite loop?", "correct_answer": "A"}
[[Indefinite]] iteration repeats code while a condition stays true, when the count is not known in advance.
{"cloze_text": "[[Indefinite]] iteration repeats code while a condition stays true, when the count is not known in advance."}
Python implements indefinite iteration with a [[while]] loop.
{"cloze_text": "Python implements indefinite iteration with a [[while]] loop."}
A [[sentinel]] is a special input value that tells the loop to stop.
{"cloze_text": "A [[sentinel]] is a special input value that tells the loop to stop."}
The [[break]] statement exits the nearest loop immediately.
{"cloze_text": "The [[break]] statement exits the nearest loop immediately."}
An [[infinite]] loop is a failure mode when the condition never becomes false.
{"cloze_text": "An [[infinite]] loop is a failure mode when the condition never becomes false."}
{"keyword": "indefinite iteration", "definition": "Repeating code while a condition is true, when you do not know the count in advance"}
{"keyword": "while loop", "definition": "A loop that checks a condition before each pass and runs the body while it is true"}
{"keyword": "break", "definition": "A statement that exits the nearest loop immediately"}
{"keyword": "continue", "definition": "A statement that skips the rest of the current iteration and starts the next one"}
{"keyword": "infinite loop", "definition": "A failure mode where the loop condition never becomes false so the loop never ends"}
Question Type: fill in the blanks
{"paragraph": "A [[while]] loop checks a [[condition]] before each pass. Use a [[sentinel]] or update a variable so the condition can become false. [[break]] exits early; [[continue]] skips to the next iteration."}
{"paragraph": "A [[while]] loop checks a [[condition]] before each pass. Use a [[sentinel]] or update a variable so the condition can become false. [[break]] exits early; [[continue]] skips to the next iteration."}
Question Type: short answer
{"answer": "Indefinite iteration means the count is not known in advance and the loop stops when a condition becomes false. An infinite loop is a failure mode where that condition never becomes false.", "rubric": {"0": "Incorrect or no response", "1": "Partial credit", "2": "Indefinite iteration means the count is not known in advance and the loop stops when a condition becomes false. An infinite loop is a failure mode where that condition never becomes false."}, "question": "Explain the difference between indefinite iteration and an infinite loop."}
{"answer": "Indefinite iteration means the count is not known in advance and the loop stops when a condition becomes false. An infinite loop is a failure mode where that condition never becomes false.", "rubric": {"0": "Incorrect or no response", "1": "Partial credit", "2": "Indefinite iteration means the count is not known in advance and the loop stops when a condition becomes false. An infinite loop is a failure mode where that condition never becomes false."}, "question": "Explain the difference between indefinite iteration and an infinite loop."}
Question Type: short answer
{"answer": "break exits the loop early (e.g. when the user types quit). continue skips the rest of the current pass (e.g. skip processing an invalid value) and starts the next iteration.", "rubric": {"0": "Incorrect or no response", "1": "Partial credit", "2": "break exits the loop early (e.g. when the user types quit). continue skips the rest of the current pass (e.g. skip processing an invalid value) and starts the next iteration."}, "question": "Describe one use of break and one use of continue in a while loop."}
{"answer": "break exits the loop early (e.g. when the user types quit). continue skips the rest of the current pass (e.g. skip processing an invalid value) and starts the next iteration.", "rubric": {"0": "Incorrect or no response", "1": "Partial credit", "2": "break exits the loop early (e.g. when the user types quit). continue skips the rest of the current pass (e.g. skip processing an invalid value) and starts the next iteration."}, "question": "Describe one use of break and one use of continue in a while loop."}
Optional comparison: counting with while
You can write a counting condition such as x < 4 with while. It works, but it looks a lot like definite iteration. If you already know you need a fixed number of numbered passes, a for loop with range is usually clearer.
x = 0
while x < 4:
print("x is", x)
x = x + 1
x is 0
x is 1
x is 2
x is 3
Keep this pattern in mind as a comparison only — prefer while when the stop rule is input, a total, or another changing state.
Review: Fill in the Blanks
refers to a type of in programming where the number of iterations is not specified in advance. The primary purpose of a loop is to repeatedly execute a block of code until a certain condition is met. There are different types of loops, including the , , and , each serving specific purposes.
A While loop is a that repeatedly executes a block of code as long as a given condition is true. The condition is evaluated at the beginning of each iteration, and if it becomes false, the loop terminates and the moves to the next statement in the program.
In some scenarios, it may be necessary to alter the flow of execution within a loop. This is achieved using specific statements such as the , which immediately terminates the loop and transfers control to the statement following the loop. Conversely, the skips the remaining code within the current iteration and proceeds to the next iteration.
An is a type of loop that runs indefinitely without ever terminating, unless interrupted forcibly. It can occur unintentionally if the loop condition is not appropriately defined or the loop control variables are not updated correctly.
Click the keywords below to fill in the blanks:
For Loop
Indefinite Iteration
Infinite Loop
Conditional Loop
Continue Statement
While Loop
Break Statement
Do-While Loop
Loop
Control Flow