Pseudocode Core Structures (Cambridge Standard): WHILE Loops And Condition Placement Errors (Copy)
WHILE Loops And Condition Placement Errors (Cambridge Standard – O Level 2210 + IGCSE 0478)
Purpose Of WHILE Loops In Cambridge Pseudocode
- WHILE loops are used when:
- The number of repetitions is not known in advance
- Cambridge uses WHILE loops to test:
- Condition-controlled repetition
- Correct condition placement
- Logical termination
- WHILE loops commonly appear in:
- Input validation
- Searching
- Sentinel-controlled processing
Core WHILE Loop Structure (Cambridge Standard)
Basic Structure
- WHILE condition DO
- statements
- ENDWHILE
Meaning Of Each Part
- condition
- A Boolean expression
- Evaluated before each iteration
- statements
- Executed only if condition is TRUE
- ENDWHILE
- Marks the end of the loop body
Execution Rule (Critical)
- The condition is checked:
- Before the first iteration
- Before every subsequent iteration
- If the condition is FALSE initially:
- The loop body never executes
WHILE Loop Vs FOR Loop (Core Difference)
| WHILE Loop | FOR Loop |
|---|---|
| Condition-based | Count-based |
| Unknown repetitions | Known repetitions |
| Risk of infinite loop | Fixed iterations |
| Requires careful condition | Boundaries explicit |
- Cambridge expects:
- WHILE loops only when necessary
- FOR loops when counts are fixed
Correct Use Cases For WHILE Loops
- Input validation
- Reading data until a sentinel value
- Searching until found
- Repeating until a condition becomes FALSE
Example: Input Validation
- INPUT score
- WHILE score < 0 OR score > 100 DO
- OUTPUT “Invalid input”
- INPUT score
- ENDWHILE
- Key examiner checks:
- INPUT before WHILE
- Re-input inside WHILE
Condition Placement Rules (Very High-Risk Area)
Rule 1: Condition Must Be Checked Before Loop Body
- WHILE loops are pre-test loops
- Do NOT assume loop body runs at least once
Rule 2: Condition Must Be Updateable Inside Loop
- At least one statement inside the loop must:
- Change a variable used in the condition
- Otherwise:
- Infinite loop occurs
Rule 3: Condition Must Reflect Termination Clearly
- Condition should describe:
- When looping continues
- Avoid vague or inverted logic
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
Common WHILE Loop Condition Placement Errors
Error 1: INPUT Inside Loop Without Initial INPUT
Incorrect:
- WHILE value <> -1 DO
- INPUT value
- ENDWHILE
Why it fails:
- value is undefined at first condition check
Correct:
- INPUT value
- WHILE value <> -1 DO
- PROCESS value
- INPUT value
- ENDWHILE
Error 2: Condition Uses Variable Never Updated
Incorrect:
- WHILE count < 10 DO
- total ← total + value
- ENDWHILE
Why it fails:
- count never changes
- Infinite loop
Correct:
- WHILE count < 10 DO
- total ← total + value
- count ← count + 1
- ENDWHILE
Error 3: Updating Condition Variable Outside Loop
Incorrect:
- WHILE score < 50 DO
- OUTPUT “Retry”
- ENDWHILE
- score ← score + 10
Why it fails:
- score never changes during looping
Error 4: Reversing Condition Logic
Incorrect:
- WHILE valid = FALSE DO
- PROCESS
- ENDWHILE
When valid is never updated:
- Infinite loop
Correct:
- WHILE NOT valid DO
- INPUT value
- valid ← value >= min AND value <= max
- ENDWHILE
WHILE Loops And Sentinel Values
Sentinel-Controlled Loop Pattern
- INPUT value
- WHILE value <> sentinel DO
- PROCESS value
- INPUT value
- ENDWHILE
Examiner Expectations
- Sentinel:
- Must be clearly stated
- Sentinel value:
- Must not be processed
- INPUT must occur:
- Before loop
- At end of loop body
Common Sentinel Errors
- Processing sentinel value
- Forgetting final INPUT
- Using sentinel without explanation
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
WHILE Loops And Searching
Search Loop Pattern
- found ← FALSE
- index ← 1
- WHILE found = FALSE AND index <= max DO
- IF data[index] = target THEN
- found ← TRUE
- ELSE
- index ← index + 1
- ENDIF
- IF data[index] = target THEN
- ENDWHILE
Examiner Checks
- Condition uses:
- found flag
- boundary control
- index increment:
- Must occur if not found
Search Loop Trap
- Forgetting boundary condition
- Results in:
- Array out-of-bounds
- Infinite loop
WHILE Loops And Validation Logic
Correct Validation Pattern
- INPUT value
- WHILE value < min OR value > max DO
- OUTPUT “Invalid input”
- INPUT value
- ENDWHILE
Incorrect Validation Patterns
- Using IF instead of WHILE
- Re-input without loop
- No re-input at all
WHILE Loops And Trace Tables
Trace Table Requirements
- Trace tables must show:
- Condition evaluation
- Variable changes per iteration
- Variables commonly traced:
- Loop condition variables
- Counters
- Flags
Trace Error Patterns
- Assuming loop runs once automatically
- Missing termination iteration
- Incorrect condition evaluation order
Written and Compiled By Sir Hunain Zia (AYLOTI), World Record Holder With 154 Total A Grades, 7 Distinctions and 11 World Records For Educate A Change O Level And IGCSE Computer Science Full Scale Course
WHILE Loop Vs REPEAT UNTIL (Cambridge Context)
- REPEAT UNTIL:
- Is a post-test loop
- Runs at least once
- WHILE:
- Is a pre-test loop
- May run zero times
- Cambridge Paper 2:
- Strongly prefers WHILE
- Uses REPEAT UNTIL rarely
Section B Modifications Involving WHILE Loops
- Common Section B tasks:
- Add extra condition
- Restrict number of iterations
- Prevent infinite looping
Safe Modification Strategy
- Keep original WHILE structure
- Modify:
- Condition only
- Do not:
- Rewrite as FOR unless asked
Illegal WHILE Loop Practices (Guaranteed Mark Loss)
- Missing ENDWHILE
- Condition variable never updated
- INPUT only inside loop without initial INPUT
- Infinite loops
- Ambiguous termination condition
Best-Practice WHILE Loop Strategy For Paper 2
- Always initialise condition variables
- Ensure condition can become FALSE
- Update condition variables inside loop
- Use clear, readable conditions
- Avoid over-complicated logic
- Prefer FOR loops if count is fixed
Final Quality Checklist
- Condition evaluated before loop
- Condition variables initialised
- Condition variables updated
- Termination guaranteed
- ENDWHILE present
- Logic traceable
Final Lock-In Rules
- WHILE loops are condition-controlled
- Condition placement determines correctness
- Infinite loops lose marks immediately
- Validation and searching rely on WHILE
- Clean WHILE logic = safe Paper 2 marks
