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 assess:
- Condition-controlled repetition
- Correct placement of conditions
- Safe termination of loops
- WHILE loops are commonly tested in:
- Input validation
- Sentinel-controlled input
- Searching algorithms
- Repeating processes until a condition changes
Core WHILE Loop Structure (Cambridge Standard)
- WHILE condition DO
- statements
- ENDWHILE
Meaning Of Each Part
- condition
- A Boolean expression
- Evaluated before every iteration
- statements
- Executed only if the condition is TRUE
- ENDWHILE
- Marks the end of the loop body
Execution Rule (Examiner-Critical)
- The condition is checked:
- Before the first iteration
- Before every subsequent iteration
- If the condition is FALSE initially:
- The loop body does not execute at all
WHILE Loop Vs FOR Loop (Examiner Perspective)
| WHILE Loop | FOR Loop |
|---|---|
| Condition-controlled | Count-controlled |
| Unknown repetitions | Known repetitions |
| High infinite-loop risk | Fixed boundaries |
| Used for validation/search | Used for arrays/fixed data |
- Cambridge expects:
- WHILE loops only when repetition count is unknown
- FOR loops when iteration count is known
Correct Use Cases For WHILE Loops
- Validating user input
- Re-entering data until valid
- Reading input until a sentinel value is entered
- Searching until a value is found or data ends
- Repeating a process until a condition becomes FALSE
Standard Input Validation Pattern (Cambridge-Safe)
- INPUT value
- WHILE value < minimum OR value > maximum DO
- OUTPUT “Invalid input”
- INPUT value
- ENDWHILE
Examiner Expectations
- Initial INPUT must occur before the WHILE loop
- Re-input must occur inside the loop
- Condition must clearly describe invalid state
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
Condition Placement Rules (High-Risk Area)
Rule 1: Condition Is Checked Before Loop Body
- WHILE is a pre-test loop
- The loop body:
- May execute zero times
- Never assume:
- At least one execution
Rule 2: Condition Variables Must Be Initialised Before WHILE
- All variables used in the condition must:
- Have defined values before the loop starts
Incorrect:
- WHILE value <> -1 DO
- INPUT value
- ENDWHILE
Correct:
- INPUT value
- WHILE value <> -1 DO
- PROCESS value
- INPUT value
- ENDWHILE
Rule 3: Condition Variables Must Be Updated Inside Loop
- At least one variable in the condition must:
- Change inside the loop
- Otherwise:
- Infinite loop occurs
Rule 4: Condition Must Represent “Continue Looping”
- The condition should describe:
- When the loop continues
- Avoid inverted or confusing logic
Better:
- WHILE value < min OR value > max DO
Not:
- WHILE NOT (value >= min AND value <= max) DO
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: Using Condition Variable Before Initial INPUT
- Condition checks an uninitialised variable
- Results in:
- Undefined logic
- Automatic mark loss
Error 2: Condition Variable Never Changes
Incorrect:
- WHILE count < 10 DO
- total ← total + value
- ENDWHILE
Why it fails:
- count never updates
- Loop never terminates
Correct:
- WHILE count < 10 DO
- total ← total + value
- count ← count + 1
- ENDWHILE
Error 3: Updating Condition Variable Outside The Loop
Incorrect:
- WHILE score < 50 DO
- OUTPUT “Retry”
- ENDWHILE
- score ← score + 10
Why it fails:
- score never changes while looping
Error 4: Wrong Condition Direction
Incorrect:
- WHILE valid = FALSE DO
- PROCESS
- ENDWHILE
When valid never changes:
- Infinite loop
Correct:
- WHILE NOT valid DO
- INPUT value
- valid ← value >= min AND value <= max
- ENDWHILE
WHILE Loops And Sentinel Values
Sentinel-Controlled Input Pattern
- INPUT value
- WHILE value <> sentinel DO
- PROCESS value
- INPUT value
- ENDWHILE
Examiner Expectations For Sentinel Loops
- Sentinel value:
- Must be clearly defined
- Sentinel must:
- Not be processed
- INPUT must appear:
- Before the loop
- At the end of loop body
Common Sentinel Errors
- Processing the 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 Algorithms
Standard Linear Search 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
- Loop condition includes:
- Found flag
- Boundary condition
- Index increments only when not found
- Search terminates safely
Search Loop Traps
- Missing boundary condition
- Forgetting to increment index
- Infinite loop when target not found
WHILE Loops And Trace Tables
Trace Table Expectations
- Trace tables must show:
- Condition evaluation per iteration
- Changes in variables used in condition
- Typical traced variables:
- Condition variables
- Counters
- Flags
Common Trace Errors
- Assuming loop runs at least once
- Skipping the terminating condition check
- Missing final iteration where condition becomes FALSE
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 Vs REPEAT UNTIL (Cambridge Context)
| WHILE | REPEAT UNTIL |
|---|---|
| Pre-test loop | Post-test loop |
| May run zero times | Runs at least once |
| Preferred by Cambridge | Rarely used |
| Safer for validation | Used only when required |
- In Paper 2:
- WHILE is the expected default
WHILE Loops In Section B Modifications
- Section B commonly asks to:
- Add extra condition
- Restrict loop execution
- Prevent infinite loops
- Correct approach:
- Modify condition only
- Keep loop structure intact
Illegal WHILE Loop Practices (Guaranteed Mark Loss)
- Missing ENDWHILE
- Using uninitialised condition variables
- No update to condition variables
- Infinite loops
- INPUT only inside loop without initial INPUT
Best-Practice WHILE Loop Strategy For Paper 2
- Initialise all condition variables
- Ensure condition can become FALSE
- Update condition variables inside loop
- Keep conditions clear and readable
- Prefer WHILE only when repetition count is unknown
Final Quality Checklist
- Condition evaluated before loop
- Condition variables initialised
- Condition variables updated
- Termination guaranteed
- ENDWHILE present
- Logic traceable step-by-step
Final Lock-In Rules
- WHILE loops are condition-controlled
- Condition placement decides correctness
- Infinite loops lose marks immediately
- Validation and searching depend on WHILE
- Clean WHILE logic = safe Paper 2 marks
