Arrays, Records & Data Handling: Traversing Arrays Using Loops (Copy)
Traversing Arrays Using Loops (Cambridge Standard – O Level 2210 + IGCSE 0478)
Purpose Of Array Traversal In Cambridge Pseudocode
- Traversing an array means:
- Accessing each element one by one using a loop
- Cambridge tests traversal to assess:
- Correct loop choice
- Safe index handling
- Accurate processing of all elements
- Array traversal is fundamental to:
- Pre-release material algorithms
- Section B modifications
- Trace table and dry-run questions
What Cambridge Expects By “Traversing An Array”
- Every valid element:
- Is accessed exactly once (unless stated otherwise)
- Traversal must:
- Respect declared array bounds
- Use correct loop control
- No element should be:
- Skipped
- Repeated unintentionally
- Accessed out of range
Core Traversal Pattern For One-Dimensional Arrays
Standard FOR Loop Traversal (Most Common)
- FOR index ← lowerBound TO upperBound
- PROCESS arrayName[index]
- ENDFOR
Example
- FOR i ← 1 TO 30
- OUTPUT marks[i]
- ENDFOR
Examiner Expectations
- Loop variable:
- Is INTEGER
- Loop bounds:
- Match array declaration exactly
- Index used:
- Matches loop variable
Why FOR Loops Are Preferred For Traversal
- Number of elements:
- Is known from array bounds
- FOR loops:
- Are inclusive
- Are predictable
- Are easier to trace
- Using WHILE unnecessarily:
- Is allowed
- But increases error risk
Traversing Arrays Using WHILE Loops (Allowed But Risky)
Standard WHILE Traversal Pattern
- index ← lowerBound
- WHILE index <= upperBound DO
- PROCESS arrayName[index]
- index ← index + 1
- ENDWHILE
Examiner Focus
- index must be:
- Initialised before loop
- Updated inside loop
- Boundary condition must:
- Match array bounds
Common WHILE Traversal Errors
- Forgetting to increment index
- Incorrect loop condition
- Infinite loop
- Skipping first or last element
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
Traversing Arrays For Input Operations
Standard Input Traversal
- FOR i ← lowerBound TO upperBound
- INPUT arrayName[i]
- ENDFOR
Example
- FOR i ← 1 TO 20
- INPUT names[i]
- ENDFOR
Examiner Expectations
- One INPUT per iteration
- No skipped indices
- Loop runs full range
Traversing Arrays For Output Operations
Standard Output Traversal
- FOR i ← lowerBound TO upperBound
- OUTPUT arrayName[i]
- ENDFOR
Example
- FOR i ← 1 TO 10
- OUTPUT prices[i]
- ENDFOR
Output Formatting Notes
- Formatting is secondary
- Correct traversal logic is primary
- Do not add unnecessary OUTPUT statements
Traversing Arrays For Accumulation
Accumulator Pattern (Very High Frequency)
- total ← 0
- FOR i ← lowerBound TO upperBound
- total ← total + arrayName[i]
- ENDFOR
Examiner Expectations
- Accumulator initialised before loop
- Update occurs inside loop
- No reset during traversal
Common Accumulation Errors
- Initialising accumulator inside loop
- Using wrong index
- Forgetting to initialise accumulator
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
Traversing Arrays With Selection (Filtering Elements)
Conditional Traversal Pattern
- FOR i ← lowerBound TO upperBound
- IF condition(arrayName[i]) THEN
- PROCESS arrayName[i]
- ENDIF
- IF condition(arrayName[i]) THEN
- ENDFOR
Example: Counting Valid Values
- count ← 0
- FOR i ← 1 TO 30
- IF marks[i] >= 50 THEN
- count ← count + 1
- ENDIF
- IF marks[i] >= 50 THEN
- ENDFOR
Examiner Focus
- Loop always runs full range
- IF controls processing, not iteration
- Condition applies per element
Traversing Arrays For Searching
Linear Search Pattern
- found ← FALSE
- index ← lowerBound
- WHILE found = FALSE AND index <= upperBound DO
- IF arrayName[index] = target THEN
- found ← TRUE
- ELSE
- index ← index + 1
- ENDIF
- IF arrayName[index] = target THEN
- ENDWHILE
Examiner Expectations
- Boundary condition included
- index increments correctly
- Search terminates safely
Common Search Traversal Errors
- Missing boundary check
- Infinite loop if target not found
- Incorrect condition 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
Traversing Arrays In Reverse Order
Reverse Traversal Using FOR
- FOR i ← upperBound TO lowerBound STEP -1
- PROCESS arrayName[i]
- ENDFOR
Example
- FOR i ← 10 TO 1 STEP -1
- OUTPUT values[i]
- ENDFOR
Examiner Focus
- STEP must be negative
- Bounds must be correct
- No off-by-one errors
Traversing Subsets Of Arrays
Partial Traversal Pattern
- FOR i ← startIndex TO endIndex
- PROCESS arrayName[i]
- ENDFOR
Examiner Expectations
- startIndex and endIndex:
- Must be within declared bounds
- Must match question requirement
Traversing Two-Dimensional Arrays (Brief Context)
- Requires:
- Nested loops
- Pattern:
- FOR row ← rowLower TO rowUpper
- FOR col ← colLower TO colUpper
- PROCESS arrayName[row, col]
- ENDFOR
- FOR col ← colLower TO colUpper
- ENDFOR
- Inner loop:
- Traverses columns
- Outer loop:
- Traverses rows
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
Traversing Arrays And Trace Tables
How Traversal Appears In Trace Questions
- Trace tables often include:
- Loop index
- Current array element
- Updated variables
Trace Rule
- One table row per iteration
- Index increases sequentially
- Array element changes with index
Common Trace Errors
- Skipping first element
- Ending traversal early
- Using incorrect index values
Traversing Arrays In Pre-Release Material
- Pre-release tasks often require:
- Traversing arrays multiple times
- Traversing arrays with different conditions
- You must:
- Preserve array bounds
- Avoid hardcoded limits
- Follow task constraints exactly
Traversing Arrays In Section B Modifications
- Section B frequently asks to:
- Add validation during traversal
- Ignore invalid elements
- Stop traversal early based on condition
Safe Modification Strategy
- Keep traversal loop unchanged
- Insert IF inside loop
- Do not alter array bounds
Example Modification
Original:
- FOR i ← 1 TO 30
- total ← total + marks[i]
- ENDFOR
Modified:
- FOR i ← 1 TO 30
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- total ← total + marks[i]
- ENDIF
- IF marks[i] >= 0 AND marks[i] <= 100 THEN
- ENDFOR
Illegal Array Traversal Practices (Guaranteed Mark Loss)
- Accessing out-of-range indices
- Modifying loop counter manually
- Using undeclared arrays
- Incorrect loop bounds
- Mixing loop variables
Best-Practice Traversal Strategy For Paper 2
- Prefer FOR loops for full traversal
- Match loop bounds to array declaration
- Initialise accumulators before traversal
- Use IF for filtering, not looping
- Trace one full traversal mentally before finalising
Final Quality Checklist
- Correct loop type chosen
- Bounds match declaration
- Index variable used consistently
- No skipped or repeated elements
- Logic traceable and safe
Final Lock-In Rules
- Traversal means accessing every required element
- FOR loops are safest for arrays
- Boundary errors are the most common mistakes
- Selection filters traversal, does not control it
- Clean traversal logic = strong Paper 2 performance
