Community resourceWorksheet
1CP2-CT-2.7 Constructing and debugging while loops
Part 7 of 8 · 1CP2-CT-2 · Selection, strings and while loops
The construction worksheet, where students choose a loop pattern from the requirement and then build it.
Students will:
- choose between a counting loop and a sentinel loop
- construct a counting loop that stops at the right value
- construct a sentinel loop that does not process its stop value
- identify and correct an off-by-one loop condition
- combine repetition with selection in one program
Inside: 8 explanation cells, 1 multiple-choice question, 3 Python tasks, 1 fill-in-the-blanks cell and 2 written answers. 20 marks, about 45 minutes.
Series: 1CP2-CT-2 · Selection, strings and while loops, part 7 of 8.
Shared by Coding PathwayVerified teacher
- 15 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Constructing and debugging while loops
A reliable loop design states the initial state, the condition for another pass, the body, and the update. This worksheet moves from completion to independent construction.
1. Choose a loop pattern from the requirement
Sentinel loop: repeat until a special value is entered.
Threshold loop: repeat while a total or counter remains below/above a boundary.
Input loop: obtain the first value before the loop, use it in the condition, and obtain the next value inside the loop.
Validation is developed later. Here, input repetition is used to practise the condition-controlled structure.
number = 1
while number gap 1 5:
print(number)
number = number gap 2 1- +
- -
- <=
- >=
2. Construct a counting loop
Set count to 3. Use a while loop to output 3, 4, 5 and 6 on separate lines. After termination, count should be 7.
# Initialise count and construct the loop.
3. Model a sentinel loop
code = input("Code or stop: " )
while code != "stop":
print(code.upper())
code = input("Code or stop: " )
The sentinel stop ends repetition and is not processed. The second input is essential: it supplies a new value before the condition is tested again.
For inputs ax4, by7, stop, state the output and explain why STOP is not printed.
Follow where the condition is checked relative to the output.
Students type their answer here.
4. Construct a sentinel loop
Repeatedly input integer reading. While it is not -1, add it to total and request the next reading. After -1, output only total.
It will be tested with 4, 7, -1 and with -1 immediately. The second test checks zero-pass behaviour.
# Initialise total, obtain the first reading, repeat, then output total.
5. Debug systematically
Read, locate, explain, amend and rerun. For loops, ask:
- Is the first condition evaluated with an initial value?
- Does the condition match continue while rather than stop when?
- Does each pass update every value on which termination depends?
- Are boundary and zero-pass tests included?
A program intended to output 1 through 5 uses while count < 5. Identify the logic error, give the correction and name a test result that confirms the boundary is now included.
Distinguish the condition from the update.
Students type their answer here.
6. Combine repetition and selection
Input integer attempts. While attempts is below 3, output Try, increment attempts, and continue. After the loop, output Finished. This retrieves selection-style conditions but the repeated behaviour is the focus.
# Complete the required loop.
Which test most directly checks zero-pass behaviour for while value < 10?
- Avalue starts at 0
- Bvalue starts at 9
- Cvalue starts at 10
- Dvalue changes by 2
Route forward
You can now construct, test and debug condition-controlled loops. The checkpoint retrieves strings, selection, Boolean logic, readability and while loops without adding a new construct.