Pseudocode Core Structures (Cambridge Standard): Combining Selection And Iteration Correctly (Copy)
Combining Selection And Iteration Correctly (Cambridge Standard – O Level 2210 + IGCSE 0478)
Why Combining Selection And Iteration Is Heavily Tested In Paper 2
- Real algorithms rarely use:
- Only selection
- Or only iteration
- Cambridge tests the ability to:
- Place IF statements inside loops
- Place loops inside selection
- Control execution flow precisely
- Most logic and trace errors occur when:
- Selection and iteration are combined incorrectly
Fundamental Combination Patterns Cambridge Uses
- IF inside FOR
- IF inside WHILE
- FOR inside IF
- WHILE inside IF
- Nested combinations (loop → selection → loop)
Each pattern has:
- A specific purpose
- A specific examiner expectation
Pattern 1: IF Inside FOR Loop (Most Common)
Purpose
- Process each item
- Apply a condition to decide action
Standard Structure
- FOR i ← start TO end
- IF condition THEN
- statements
- ENDIF
- IF condition THEN
- ENDFOR
Typical Use Cases
- Counting valid values
- Filtering records
- Conditional accumulation
- Identifying highest / lowest values
Example
- passCount ← 0
- FOR i ← 1 TO totalStudents
- IF mark[i] >= 50 THEN
- passCount ← passCount + 1
- ENDIF
- IF mark[i] >= 50 THEN
- ENDFOR
Examiner Expectations
- Loop runs fixed number of times
- IF controls processing, not looping
- Loop variable must not be modified
Common Errors
- Putting ENDFOR before ENDIF
- Modifying loop variable inside IF
- Forgetting ENDIF
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
Pattern 2: IF Inside WHILE Loop
Purpose
- Repeat until a condition changes
- Apply decision logic during repetition
Standard Structure
- WHILE condition DO
- IF subCondition THEN
- statements
- ENDIF
- update condition variable
- IF subCondition THEN
- ENDWHILE
Typical Use Cases
- Searching until found
- Validation with multiple rules
- Processing unknown number of inputs
Example: Linear Search
- 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 Expectations
- Condition variables updated inside loop
- IF does not replace loop condition
- Clear termination logic
Common Errors
- Forgetting to update condition variable
- Using IF instead of WHILE for repetition
- Infinite loops due to unchanged condition
Pattern 3: FOR Loop Inside IF Statement
Purpose
- Perform iteration only when a condition is met
Standard Structure
- IF condition THEN
- FOR i ← start TO end
- statements
- ENDFOR
- FOR i ← start TO end
- ENDIF
Typical Use Cases
- Optional processing
- Conditional batch operations
- Menu-based actions
Example
- IF option = 1 THEN
- FOR i ← 1 TO totalRecords
- OUTPUT record[i]
- ENDFOR
- FOR i ← 1 TO totalRecords
- ENDIF
Examiner Expectations
- IF condition checked once
- FOR loop executes fully if condition TRUE
- No partial looping logic
Common Errors
- Replacing IF with WHILE unnecessarily
- Expecting loop to re-check IF condition
- Misplacing ENDIF / ENDFOR
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
Pattern 4: WHILE Loop Inside IF Statement
Purpose
- Start repetition only if a condition is satisfied
- Common in:
- Input-controlled execution
- Error-handling logic
Standard Structure
- IF condition THEN
- WHILE loopCondition DO
- statements
- ENDWHILE
- WHILE loopCondition DO
- ENDIF
Example
- IF retry = TRUE THEN
- WHILE attempts < maxAttempts DO
- INPUT value
- attempts ← attempts + 1
- ENDWHILE
- WHILE attempts < maxAttempts DO
- ENDIF
Examiner Expectations
- IF controls entry
- WHILE controls repetition
- Conditions must not overlap unnecessarily
Common Errors
- Duplicating same condition in IF and WHILE
- Creating unreachable loops
- Confusing entry condition with termination condition
Pattern 5: Nested Combination (Loop Inside Loop With Selection)
Purpose
- Multi-level processing
- Comparing datasets
- Aggregated logic
Example Structure
- FOR i ← 1 TO rows
- FOR j ← 1 TO columns
- IF matrix[i][j] > threshold THEN
- count ← count + 1
- ENDIF
- IF matrix[i][j] > threshold THEN
- ENDFOR
- FOR j ← 1 TO columns
- ENDFOR
Examiner Expectations
- Inner loop completes fully for each outer loop iteration
- Correct variable usage for each loop
- No cross-modification of loop counters
Common Errors
- Using same variable for both loops
- Modifying outer loop counter inside inner loop
- Incorrect indentation causing logic confusion
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
Selection And Iteration In Validation Logic
Correct Validation With Loop And IF
- REPEAT
- INPUT value
- IF value < min OR value > max THEN
- OUTPUT “Invalid input”
- ENDIF
- UNTIL value >= min AND value <= max
Why This Works
- Loop ensures repetition
- IF handles messaging
- UNTIL handles termination
Common Validation Errors
- Using IF without loop
- Looping without condition check
- Logical reversal in UNTIL
Selection And Iteration In Accumulation Tasks
Correct Pattern
- total ← 0
- FOR i ← 1 TO n
- IF valid[i] = TRUE THEN
- total ← total + value[i]
- ENDIF
- IF valid[i] = TRUE THEN
- ENDFOR
Examiner Focus
- Accumulator initialised before loop
- IF filters inclusion
- Loop controls iteration count
Trace Table Impact When Combining Structures
- Each iteration:
- Executes selection independently
- Trace tables must show:
- Loop counter values
- Variable updates only when IF condition TRUE
Common Trace Errors
- Assuming IF runs every iteration
- Updating variables when condition FALSE
- Missing iterations due to incorrect loop bounds
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
Section B Modification Risks With Combined Structures
- Section B often asks to:
- Add a condition inside an existing loop
- Restrict iteration based on selection
- Safe strategy:
- Insert IF inside loop
- Do not rewrite loop structure
Example Modification
Original:
- FOR i ← 1 TO 30
- total ← total + score[i]
- ENDFOR
Modified:
- FOR i ← 1 TO 30
- IF score[i] >= 0 AND score[i] <= 100 THEN
- total ← total + score[i]
- ENDIF
- IF score[i] >= 0 AND score[i] <= 100 THEN
- ENDFOR
Illegal Combination Practices (Guaranteed Mark Loss)
- Modifying loop counter inside IF
- Using IF where loop is required
- Infinite loops caused by missing updates
- Misplaced ENDIF / ENDFOR / ENDWHILE
- Over-nesting without logical need
Best-Practice Rules For Combining Selection And Iteration
- Decide first:
- Is repetition required?
- Is decision required?
- Choose:
- Loop for repetition
- IF for decision
- Nest only when logically necessary
- Keep conditions simple and readable
- Indentation must reflect logic clearly
Final Quality Checklist
- Loop controls repetition
- IF controls decision-making
- No variable serves conflicting roles
- Loop counters untouched
- Termination guaranteed
- Structure matches pre-release intent
Final Lock-In Rules
- Selection decides what happens
- Iteration decides how many times
- Mixing roles causes logic failure
- Correct nesting = high Paper 2 marks
- Clean combined logic is examiner-friendly
