Arrays, Strings And Data Handling: Traversing Arrays Using Loops (Copy)
Traversing Arrays Using Loops
What Traversing An Array Means
- Traversing an array means:
- Visiting every element in the array
- One element at a time
- In a controlled, logical order
- Traversal is used to:
- Input values
- Output values
- Search
- Count
- Modify data
- Calculate totals, averages, maximums, minimums
Core Traversal Rule (Exam-Critical)
- Every valid index must be visited exactly once unless stated otherwise
- Traversal almost always uses:
FORloop (most common)WHILEloop (conditional traversal)- Nested loops (for 2D arrays)
Traversing One-Dimensional Arrays Using FOR Loops
Standard Traversal Template (MOST USED)
FOR i ← 1 TO n
<process array[i]>
NEXT i
- Why FOR is preferred:
- Fixed number of elements
- Clear bounds
- Examiner-friendly
Example 1: Output All Elements
FOR i ← 1 TO n
OUTPUT arr[i]
NEXT i
- Traverses:
- From first element to last
- Pitfall:
- Using wrong upper bound
Example 2: Input Values Into An Array
FOR i ← 1 TO n
INPUT arr[i]
NEXT i
- Examiner expects:
- Loop limits match declared array size
Example 3: Calculating Total Of Elements
total ← 0
FOR i ← 1 TO n
total ← total + arr[i]
NEXT i
- Key point:
- Accumulator initialised before traversal
Example 4: Counting Based On Condition
count ← 0
FOR i ← 1 TO n
IF arr[i] >= 50 THEN
count ← count + 1
ENDIF
NEXT i
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 AS Level Computer Science Full Scale Course
Traversing Using WHILE Loops
When WHILE Is Used
- Traversal should:
- Stop early
- Depend on a condition
- Common in:
- Searching
- Validation
Standard WHILE Traversal Template
index ← 1
WHILE index <= n DO
<process arr[index]>
index ← index + 1
ENDWHILE
- Pitfall:
- Forgetting to increment index → infinite loop
Example 5: Linear Search Traversal
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
- Traversal stops:
- When element found
- Or when array ends
Traversing Using REPEAT UNTIL Loops
When REPEAT UNTIL Is Appropriate
- Must traverse at least once
- Exit condition checked after processing
Example 6: Guaranteed First Traversal
index ← 1
REPEAT
OUTPUT arr[index]
index ← index + 1
UNTIL index > n
- Pitfall:
- Not protecting array bounds properly
Traversing Arrays Backwards
Reverse Traversal Pattern
FOR i ← n TO 1 STEP -1
OUTPUT arr[i]
NEXT i
- Common exam use:
- Reverse output
- Reverse processing
Reverse Traversal With Condition
FOR i ← n TO 1 STEP -1
IF arr[i] < 0 THEN
arr[i] ← 0
ENDIF
NEXT i
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 AS Level Computer Science Full Scale Course
Traversing Two-Dimensional Arrays (Tables)
Core Rule For 2D Traversal
- Nested loops are mandatory
- One loop controls rows
- One loop controls columns
Standard 2D Traversal Template
FOR row ← 1 TO r
FOR col ← 1 TO c
<process table[row, col]>
NEXT col
NEXT row
Example 7: Output Entire Table
FOR row ← 1 TO r
FOR col ← 1 TO c
OUTPUT table[row, col]
NEXT col
NEXT row
Example 8: Row-Wise Processing
FOR row ← 1 TO r
total ← 0
FOR col ← 1 TO c
total ← total + table[row, col]
NEXT col
OUTPUT total
NEXT row
- Key examiner point:
- Reset accumulator inside outer loop
Example 9: Column-Wise Processing
FOR col ← 1 TO c
total ← 0
FOR row ← 1 TO r
total ← total + table[row, col]
NEXT row
OUTPUT total
NEXT col
Conditional Traversal Of Arrays
Skipping Certain Elements
FOR i ← 1 TO n
IF arr[i] MOD 2 = 0 THEN
OUTPUT arr[i]
ENDIF
NEXT i
- Traversal still occurs fully
- Processing is conditional
Traversing Until Condition Met
index ← 1
WHILE index <= n AND arr[index] <> target DO
index ← index + 1
ENDWHILE
- Stops traversal early
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 AS Level Computer Science Full Scale Course
Common Traversal Errors (High-Frequency)
Error 1: Off-By-One Error
FOR i ← 1 TO n + 1
- Causes:
- Out-of-range access
Error 2: Wrong Loop Variable
FOR i ← 1 TO n
OUTPUT arr[j]
NEXT i
jnot changing → logic error
Error 3: Traversing Uninitialised Array
- Accessing values before input
- Examiner treats as undefined behaviour
Error 4: Missing Update In WHILE
WHILE index <= n DO
OUTPUT arr[index]
ENDWHILE
- Infinite loop
Dry-Run Technique For Array Traversal
Step-by-Step Method
- Write index values vertically
- Track array element accessed each iteration
- Confirm index stays within bounds
- Confirm update occurs every cycle
Exam-Focused Traversal Patterns (Quick Table)
| Task | Loop Type |
|---|---|
| Input all values | FOR |
| Output all values | FOR |
| Sum / count | FOR |
| Search | WHILE |
| Reverse order | FOR STEP -1 |
| Table traversal | Nested FOR |
Safe Traversal Templates (Copy-Paste Ready)
Template 1: Full Traversal
FOR i ← 1 TO n
<process arr[i]>
NEXT i
Template 2: Conditional Traversal
FOR i ← 1 TO n
IF <condition> THEN
<process arr[i]>
ENDIF
NEXT i
Template 3: Early Exit Traversal
index ← 1
WHILE index <= n AND <condition> DO
index ← index + 1
ENDWHILE
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 AS Level Computer Science Full Scale Course
Examiner Checklist For Full Marks
- Correct loop type chosen
- Loop bounds match array size
- Index updated correctly
- No out-of-range access
- Correct use of nested loops for tables
One-Line Rules To Memorise
- Traversal means visit every element
- FOR is safest for arrays
- WHILE allows early exit
- Nested loops for 2D arrays
- Bounds matter more than logic
