Pseudocode Core Structures (Cambridge Standard): FOR Loops: Start, End, Step And Boundary Control (Copy)
FOR Loops: Start, End, Step And Boundary Control (Cambridge Standard – O Level 2210 + IGCSE 0478)
Purpose Of FOR Loops In Cambridge Pseudocode
- FOR loops are used when:
- The number of iterations is known before execution
- Cambridge assesses FOR loops to test:
- Accurate boundary control
- Correct iteration count
- Safe array traversal
- Clean traceability
- FOR loops commonly appear in:
- Processing fixed-size datasets
- Traversing arrays
- Repeating calculations a set number of times
Core FOR Loop Structure (Cambridge-Approved)
- FOR counter ← start TO end
- statements
- ENDFOR
Meaning Of Each Component
- counter
- Loop control variable
- Must be INTEGER
- start
- Initial value assigned to counter
- end
- Final value at which the loop stops
- statements
- Executed once per iteration
Execution Rules (Examiner-Critical)
- The loop:
- Starts with counter = start
- Ends when counter reaches end
- The loop:
- Includes both start and end values
- Number of iterations:
- (end − start) + 1 (when STEP = 1)
Inclusive Nature Of FOR Loops (High-Risk Area)
- Cambridge FOR loops are inclusive
- Example:
- FOR i ← 1 TO 5
- Iterations:
- i = 1, 2, 3, 4, 5
- Total iterations:
- 5
Common Examiner Trap
- Treating end as exclusive
- Leads to:
- Off-by-one errors
- Incorrect trace tables
- Incorrect totals or counts
STEP In FOR Loops (Increment Control)
Structure With STEP
- FOR counter ← start TO end STEP stepValue
- statements
- ENDFOR
Meaning Of STEP
- STEP controls:
- How much the counter changes after each iteration
- If STEP is not written:
- Default is STEP 1
STEP Examples
- FOR i ← 1 TO 10
- Iterates 10 times
- FOR i ← 2 TO 10 STEP 2
- Values: 2, 4, 6, 8, 10
- FOR i ← 10 TO 1 STEP -1
- Values: 10, 9, 8, …, 1
STEP Rules Examiners Enforce
- STEP must:
- Match direction of loop
- Invalid combinations:
- Positive STEP with decreasing range
- Negative STEP with increasing range
- Such errors cause:
- Loop to run zero times
- Or infinite confusion in traces
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
FOR Loops And Array Boundary Control
Standard Array Traversal Pattern
- DECLARE score[1:30]
- FOR i ← 1 TO 30
- PROCESS score[i]
- ENDFOR
Examiner Boundary Checks
- Loop bounds must:
- Match array index range exactly
- Indexing errors lead to:
- Invalid memory access (conceptually)
- Lost marks in trace questions
Common Array Boundary Errors
- Starting loop at 0 when array starts at 1
- Loop exceeding declared upper bound
- Using wrong loop variable for indexing
- Hardcoding values instead of using array size
FOR Loops And Accumulators
Accumulator Pattern
- total ← 0
- FOR i ← 1 TO n
- total ← total + value[i]
- ENDFOR
Examiner Expectations
- Accumulator must:
- Be initialised before the loop
- Accumulator update:
- Occurs inside the loop
Accumulator Trap
- Initialising accumulator inside loop:
- Resets value every iteration
- Produces incorrect final result
FOR Loops And Counters
Counter Pattern
- count ← 0
- FOR i ← 1 TO n
- IF condition THEN
- count ← count + 1
- ENDIF
- IF condition THEN
- ENDFOR
Difference Between Loop Variable And Counter
- Loop variable:
- Controls iteration
- Counter:
- Tracks events or matches
- Do NOT:
- Modify loop variable manually
- This causes:
- Undefined behaviour
- Immediate mark loss
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
Boundary Control Scenarios Cambridge Tests
Scenario 1: “Process Exactly N Items”
- Correct:
- FOR i ← 1 TO N
Scenario 2: “Process Up To N Items”
- Meaning:
- Maximum N
- Implementation:
- FOR i ← 1 TO currentCount
- Do NOT assume:
- Exactly N items exist
Scenario 3: “Process Every Second Item”
- Correct:
- STEP 2
- Example:
- FOR i ← 1 TO 20 STEP 2
Scenario 4: Reverse Processing
- Correct:
- Negative STEP
- Example:
- FOR i ← max TO 1 STEP -1
Boundary Control And Pre-Release Constraints
- FOR loops must respect:
- Maximum limits stated in pre-release
- Record counts
- Data size restrictions
- Ignoring constraints:
- Loses marks even if loop syntax is correct
FOR Loops And Trace Tables
Why FOR Loops Dominate Trace Questions
- Predictable iteration
- Clear variable progression
- Easy mark allocation for examiners
Trace Table Setup Rules
- Include columns for:
- Loop counter
- Variables updated inside loop
- Exclude:
- Unchanged variables
Common Trace Errors
- Forgetting inclusive end
- Misapplying STEP
- Skipping first or last iteration
- Updating wrong variable
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
FOR Loops In Section B Modifications
- Section B commonly asks to:
- Restrict loop execution
- Add conditional processing
- Stop early based on condition
Safe Modification Strategy
- Keep FOR loop structure
- Add:
- IF condition
- Counter or flag
- Do NOT:
- Replace FOR with WHILE unless explicitly asked
Example Modification
Original:
- FOR i ← 1 TO 30
- PROCESS
- ENDFOR
Modified:
- count ← 0
- FOR i ← 1 TO 30
- IF valid THEN
- PROCESS
- count ← count + 1
- ENDIF
- IF count = 10 THEN
- EXIT LOOP
- ENDIF
- IF valid THEN
- ENDFOR
Illegal FOR Loop Practices (Guaranteed Mark Loss)
- Changing loop counter inside loop
- Using non-integer counter
- Missing ENDFOR
- Loop bounds not matching data
- Using STEP incorrectly
Best-Practice FOR Loop Strategy For Paper 2
- Use FOR loops only when count is known
- Always:
- Match boundaries to data
- Initialise accumulators outside loop
- Keep loop body simple
- Use STEP only when logically required
- Double-check boundaries before moving on
Final Quality Checklist
- Start value correct
- End value correct
- STEP value correct
- Loop variable unchanged inside loop
- Accumulators initialised correctly
- Loop respects pre-release constraints
Final Lock-In Rules
- FOR loops are inclusive
- Boundaries decide correctness
- STEP controls iteration size and direction
- Boundary mistakes cost easy marks
- Clean FOR loop logic leads to high Paper 2 accuracy
