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 repetitions is known in advance
- Cambridge uses FOR loops to test:
- Controlled iteration
- Boundary accuracy
- Trace precision
- FOR loops are heavily examined in:
- Array processing
- Counting records
- Fixed-range calculations
Core FOR Loop Structure (Cambridge Standard)
Basic Structure
- FOR counter ← start TO end
- statements
- ENDFOR
Meaning Of Each Part
- counter
- Loop control variable
- Usually INTEGER
- start
- Initial value assigned to counter
- end
- Final value at which loop stops
- statements
- Code executed once per iteration
Execution Rule
- The loop:
- Starts at start
- Ends when counter reaches end
- Includes both start and end values
Inclusive Nature Of FOR Loops (Very Important)
- Cambridge FOR loops are inclusive
- Example:
- FOR i ← 1 TO 5
- Loop runs:
- 5 times (i = 1, 2, 3, 4, 5)
Common Examiner Trap
- Treating end value as exclusive
- This causes:
- One extra iteration
- Or one missing iteration
- Both result in:
- Wrong trace tables
- Incorrect totals
FOR Loop With STEP (Increment Control)
Structure With STEP
- FOR counter ← start TO end STEP stepValue
- statements
- ENDFOR
Meaning Of STEP
- STEP controls:
- How much the counter changes each iteration
- Default STEP:
- +1 (if STEP not specified)
Examples
- FOR i ← 1 TO 10 STEP 1
- Executes 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
Examiner Expectations For STEP
- STEP must:
- Match the logical requirement
- Be consistent with start and end
- Wrong STEP value:
- Produces incorrect number of iterations
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 Traversal
Standard Array Processing Pattern
- FOR i ← 1 TO arraySize
- PROCESS array[i]
- ENDFOR
Examiner Checks
- Loop bounds must:
- Match array declaration
- Example:
- DECLARE score[1:30]
- FOR i ← 1 TO 30
Common Array Boundary Errors
- Loop exceeding array size
- Loop starting at 0 when array starts at 1
- Using wrong loop variable for indexing
FOR Loops And Accumulators
Accumulator Pattern
- total ← 0
- FOR i ← 1 TO n
- total ← total + value[i]
- ENDFOR
Key Rules
- Accumulator must:
- Be initialised before the loop
- Accumulator update:
- Happens inside the loop
Examiner Trap
- Initialising total inside the loop
- This resets the accumulator every iteration
FOR Loops And Counters
Counting Pattern
- count ← 0
- FOR i ← 1 TO n
- IF condition THEN
- count ← count + 1
- ENDIF
- IF condition THEN
- ENDFOR
Difference Between Counter And Loop Variable
- Loop variable (i):
- Controls repetition
- Counter (count):
- Tracks events
FOR Loops And Conditional Logic
- FOR loops often contain:
- IF statements
- This allows:
- Selective processing
Example:
- FOR i ← 1 TO 20
- IF mark[i] >= 50 THEN
- passCount ← passCount + 1
- ENDIF
- IF mark[i] >= 50 THEN
- ENDFOR
Examiner Expectation
- Conditions inside loops:
- Must not alter loop control
- Never change:
- Loop counter inside loop body
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 In FOR Loops (High-Risk Area)
What Boundary Control Means
- Correctly setting:
- Start value
- End value
- STEP
- Ensuring:
- No extra iterations
- No missing iterations
Boundary Control Checklist
- Start value matches:
- First valid data item
- End value matches:
- Last valid data item
- STEP direction matches:
- Increasing or decreasing logic
Example Boundary Analysis
- “Process up to 30 records”
- Correct:
- FOR i ← 1 TO 30
- Correct:
- “Process first 10 records only”
- Correct:
- FOR i ← 1 TO 10
- Correct:
- “Process every second record”
- Correct:
- STEP 2
- Correct:
Boundary Trap: “Up To”
- “Up to N” means:
- Maximum N
- Do NOT assume:
- Exactly N
- Loop must reflect:
- Current count if used
FOR Loops And Pre-Release Constraints
- FOR loops must respect:
- Maximum limits
- Data size constraints
- Ignoring constraints:
- Loses marks even if loop logic is correct
FOR Loops And Trace Tables
Why FOR Loops Dominate Trace Questions
- Each iteration:
- Produces predictable variable changes
- Trace tables often track:
- Loop variable
- Accumulators
- Counters
Trace Table Setup Rule
- Include columns for:
- Loop counter
- Variables updated inside loop
- Exclude:
- Unchanged variables
Common Trace Errors With FOR Loops
- Forgetting inclusive end value
- Misapplying STEP
- Updating wrong variable
- Skipping first or last iteration
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 Vs WHILE Loops (Examiner Comparison)
| FOR Loop | WHILE Loop |
|---|---|
| Known iterations | Unknown iterations |
| Fixed boundaries | Condition-based |
| Cleaner traces | More complex traces |
| Preferred when possible | Used when necessary |
- Cambridge prefers:
- FOR loops when iteration count is fixed
FOR Loops In Section B Modifications
- Section B often asks:
- Restrict loop
- Extend loop
- Stop early
Correct Modification Approach
- Keep:
- FOR structure
- Add:
- IF condition
- Counter limit
- Do NOT:
- Replace FOR with WHILE unless 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 loop counter
- Loop bounds not matching data
- Missing ENDFOR
- Ambiguous start/end values
Best-Practice FOR Loop Strategy For Paper 2
- Use FOR loops when:
- Count is known
- Always:
- Match array size
- Initialise accumulators before loop
- Keep loop body simple
- Use STEP only when required
- Double-check boundaries before moving on
Final Quality Checklist
- Start value correct
- End value correct
- STEP correct
- Loop variable not modified inside loop
- Accumulators initialised outside loop
- Loop matches pre-release constraints
Final Lock-In Rules
- FOR loops are inclusive
- Boundaries decide correctness
- STEP controls iteration flow
- Boundary errors cost easy marks
- Clean FOR loop logic = high Paper 2 accuracy
