Standard Algorithm & Logic Patterns: Common Algorithm Templates Used In Exams (Copy)
Common Algorithm Templates Used In Exams
Why Algorithm Templates Matter
- Exam questions often recycle logic patterns
- Different scenarios, same underlying structure
- Recognising templates helps you:
- Write faster
- Avoid logical mistakes
- Secure method marks even if final answer is wrong
Core Examiner Rule (Very Important)
- Marks are awarded for:
- Correct structure
- Correct logic flow
- Correct use of loops, conditions, variables
- Even partial templates earn marks
Template 1: Simple Input → Process → Output (IPO)
When Used
- Basic calculations
- Single-value processing
- Straightforward problems
Standard IPO Template
INPUT value
result ← process(value)
OUTPUT result
Example: Square A Number
INPUT num
square ← num * num
OUTPUT square
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
Template 2: Fixed-Count Loop Processing
When Used
- Known number of inputs
- “Input 10 values…”
- “For each student…”
Standard Fixed Loop Template
FOR i ← 1 TO n
INPUT value
PROCESS value
NEXT i
Example: Display Double Of Each Input
FOR i ← 1 TO 5
INPUT num
OUTPUT num * 2
NEXT i
Template 3: Counting Template
When Used
- “How many…”
- “Count the number of…”
Standard Counting Template
count ← 0
FOR i ← 1 TO n
INPUT value
IF condition THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Example: Count Values Greater Than 50
count ← 0
FOR i ← 1 TO 10
INPUT mark
IF mark > 50 THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
Template 4: Accumulation (Total / Sum)
When Used
- “Find total…”
- “Calculate sum…”
Standard Accumulation Template
total ← 0
FOR i ← 1 TO n
INPUT value
total ← total + value
NEXT i
OUTPUT total
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
Template 5: Average Calculation
When Used
- “Find the average…”
- “Calculate mean…”
Standard Average Template (All Values)
total ← 0
FOR i ← 1 TO n
INPUT value
total ← total + value
NEXT i
average ← total / n
OUTPUT average
Average Of Selected Values
total ← 0
count ← 0
FOR i ← 1 TO n
INPUT value
IF condition THEN
total ← total + value
count ← count + 1
ENDIF
NEXT i
IF count > 0 THEN
average ← total / count
OUTPUT average
ENDIF
Template 6: Maximum / Minimum Finder
When Used
- “Find largest…”
- “Find smallest…”
Max / Min Template (Exam-Safe)
INPUT value
max ← value
min ← value
FOR i ← 2 TO n
INPUT value
IF value > max THEN
max ← value
ENDIF
IF value < min THEN
min ← value
ENDIF
NEXT i
OUTPUT max
OUTPUT min
Template 7: Validation Using REPEAT UNTIL
When Used
- Input must be within limits
- Menu options
- Marks, ages, ranges
Validation Template
REPEAT
INPUT value
UNTIL value >= min AND value <= max
Example: Validate Mark
REPEAT
INPUT mark
UNTIL mark >= 0 AND mark <= 100
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
Template 8: Sentinel-Controlled Input
When Used
- Unknown number of inputs
- “Enter values until -1”
Sentinel Template
REPEAT
INPUT value
IF value <> sentinel THEN
PROCESS value
ENDIF
UNTIL value = sentinel
Example: Sum Until -1
total ← 0
REPEAT
INPUT num
IF num <> -1 THEN
total ← total + num
ENDIF
UNTIL num = -1
OUTPUT total
Template 9: Linear Search (With Flag)
When Used
- Searching for a value
- Checking existence
Linear Search Template
found ← FALSE
i ← 1
WHILE i <= n 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
Template 10: Bubble Sort (Standard)
When Used
- Sorting values
- Trace-table questions
Bubble Sort Template
FOR pass ← 1 TO n - 1
FOR j ← 1 TO n - 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
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
Template 11: Flag-Controlled Early Exit
When Used
- Stop loop once condition met
- Improve efficiency
Flag-Controlled Loop Template
flag ← FALSE
i ← 1
WHILE i <= n AND flag = FALSE DO
IF condition THEN
flag ← TRUE
ELSE
i ← i + 1
ENDIF
ENDWHILE
Template 12: Nested Loop Processing
When Used
- Tables
- Grids
- Multi-dimensional data
Nested Loop Template
FOR row ← 1 TO rows
FOR col ← 1 TO cols
PROCESS table[row][col]
NEXT col
NEXT row
Template 13: Combined Processing Template (Very High Frequency)
When Used
- Long algorithm questions
- Multiple outputs required
Combined Template
INPUT value
max ← value
min ← value
total ← value
count ← 1
FOR i ← 2 TO n
INPUT value
IF value > max THEN
max ← value
ENDIF
IF value < min THEN
min ← value
ENDIF
total ← total + value
count ← count + 1
NEXT i
average ← total / count
OUTPUT max
OUTPUT min
OUTPUT average
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 Template Selection Mistakes
Mistake 1: Using Fixed Loop Instead Of Sentinel
- When number of inputs is unknown
Mistake 2: Using Counter Instead Of Flag
- When only detection is required
Mistake 3: Processing Before Validation
- Always validate first
Mistake 4: Overcomplicating Simple Problems
- Simple template = full marks
Examiner Language That Signals Correct Template Use
- “A loop is used to process each value”
- “A counter tracks the number of occurrences”
- “An accumulator maintains a running total”
- “A Boolean flag controls early termination”
- “A sentinel value ends input”
Master Exam Template (Safe Default)
initialise variables
REPEAT / FOR / WHILE
INPUT
IF condition THEN
PROCESS
ENDIF
END LOOP
FINAL CALCULATION
OUTPUT
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
One-Line Rules To Memorise
- Exams repeat logic, not creativity
- Templates save time and marks
- Structure earns method marks
- Choose the simplest valid template
- Clear logic beats clever code
