Coding Sheets, Practice & Learning Support: Revision-Friendly Coding Examples Collection (Copy)
Revision-Friendly Coding Examples Collection
How To Use This Collection
- Each example shows:
- Problem intent
- Minimal, exam-safe pseudocode
- Key revision takeaway
- Designed for:
- Fast recall before exams
- Pattern recognition
- Clean reproduction under time pressure
Example 1: Fixed Count Input And Sum
Task
- Input 10 numbers
- Output their total
total ← 0
FOR i ← 1 TO 10
INPUT num
total ← total + num
NEXT i
OUTPUT total
Key Takeaway
- Accumulator initialised before loop
- Update inside loop
Example 2: Counting With Condition
Task
- Count how many numbers are greater than 50 (out of 8)
count ← 0
FOR i ← 1 TO 8
INPUT num
IF num > 50 THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Key Takeaway
- Counters increase by 1 only
- Condition inside loop
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
Example 3: Average Of Selected Values
Task
- Input 12 marks
- Find average of marks ≥ 40
total ← 0
count ← 0
FOR i ← 1 TO 12
INPUT mark
IF mark >= 40 THEN
total ← total + mark
count ← count + 1
ENDIF
NEXT i
IF count > 0 THEN
average ← total / count
OUTPUT average
ENDIF
Key Takeaway
- Divide by count, not total inputs
- Prevent division by zero
Example 4: Maximum Value Finder
Task
- Find largest of 6 numbers
INPUT num
max ← num
FOR i ← 2 TO 6
INPUT num
IF num > max THEN
max ← num
ENDIF
NEXT i
OUTPUT max
Key Takeaway
- Initialise max using first input
- No guessing starting values
Example 5: Minimum Value Finder
Task
- Find smallest of 6 numbers
INPUT num
min ← num
FOR i ← 2 TO 6
INPUT num
IF num < min THEN
min ← num
ENDIF
NEXT i
OUTPUT min
Key Takeaway
- Logic mirrors maximum
- Only comparison sign changes
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
Example 6: Combined Max, Min And Average
Task
- Input 5 values
- Output max, min and average
INPUT value
max ← value
min ← value
total ← value
FOR i ← 2 TO 5
INPUT value
IF value > max THEN
max ← value
ENDIF
IF value < min THEN
min ← value
ENDIF
total ← total + value
NEXT i
average ← total / 5
OUTPUT max
OUTPUT min
OUTPUT average
Key Takeaway
- One loop can solve multiple tasks
- Efficient and examiner-friendly
Example 7: Validation Using REPEAT UNTIL
Task
- Input mark between 0 and 100
REPEAT
INPUT mark
UNTIL mark >= 0 AND mark <= 100
OUTPUT mark
Key Takeaway
- Validation loop before processing
- AND used for valid range
Example 8: Sentinel-Controlled Total
Task
- Input numbers until -1
- Output total (exclude -1)
total ← 0
REPEAT
INPUT num
IF num <> -1 THEN
total ← total + num
ENDIF
UNTIL num = -1
OUTPUT total
Key Takeaway
- Sentinel stops input
- Sentinel never processed
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
Example 9: Linear Search With Flag
Task
- Search for target in array of 7 values
found ← FALSE
i ← 1
WHILE i <= 7 AND found = FALSE DO
IF arr[i] = target THEN
found ← TRUE
ELSE
i ← i + 1
ENDIF
ENDWHILE
IF found = TRUE THEN
OUTPUT "Found"
ELSE
OUTPUT "Not Found"
ENDIF
Key Takeaway
- Flag enables early exit
- Two loop conditions control flow
Example 10: Bubble Sort (Ascending)
Task
- Sort array of 5 numbers
FOR pass ← 1 TO 4
FOR j ← 1 TO 5 - pass
IF arr[j] > arr[j + 1] THEN
temp ← arr[j]
arr[j] ← arr[j + 1]
arr[j + 1] ← temp
ENDIF
NEXT j
NEXT pass
Key Takeaway
- Inner loop shrinks each pass
- Adjacent comparison only
Example 11: Array Traversal And Sum
Task
- Sum values in array
sum ← 0
FOR i ← 1 TO n
sum ← sum + arr[i]
NEXT i
OUTPUT sum
Key Takeaway
- Use index variable correctly
- Avoid fixed index mistake
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
Example 12: String Traversal (Character Check)
Task
- Count vowels in a string
count ← 0
INPUT text
FOR i ← 1 TO LENGTH(text)
IF text[i] = "A" OR text[i] = "E" OR text[i] = "I" OR
text[i] = "O" OR text[i] = "U" THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Key Takeaway
- Use LENGTH for loop bound
- Character-by-character access
Example 13: Flag Used For Validation
Task
- Accept valid PIN (1000–9999)
valid ← FALSE
WHILE valid = FALSE DO
INPUT pin
IF pin >= 1000 AND pin <= 9999 THEN
valid ← TRUE
ENDIF
ENDWHILE
OUTPUT "Access Granted"
Key Takeaway
- Flag controls loop exit
- Clean validation logic
Example 14: Nested Loop (2D Table Sum)
Task
- Sum values in 2×3 table
total ← 0
FOR row ← 1 TO 2
FOR col ← 1 TO 3
total ← total + table[row][col]
NEXT col
NEXT row
OUTPUT total
Key Takeaway
- Row loop outside
- Column loop inside
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
Example 15: Combined Flag + Sentinel
Task
- Search for target until found or -1 entered
found ← FALSE
REPEAT
INPUT num
IF num = target THEN
found ← TRUE
ENDIF
UNTIL num = -1 OR found = TRUE
IF found = TRUE THEN
OUTPUT "Found"
ELSE
OUTPUT "Not Found"
ENDIF
Key Takeaway
- Multiple stopping conditions allowed
- Clear logical OR
Ultra-Quick Revision Rules
- Initialise before looping
- Validate before processing
- Flags detect, counters count
- Sentinels stop input, not data
- Simple structure = maximum marks
