Selection And Iteration Code Guides: Combining Selection And Iteration Correctly (Copy)
Combining Selection And Iteration Correctly
What “Combining Selection And Iteration” Means
- Iteration controls how many times code runs
- Selection controls what decision is made each time
- Correct combination means:
- Loop controls repetition
- IF / ELSE controls logic inside each repetition
- This combination appears in:
- Almost every medium to large exam question
- Array processing
- Validation
- Searching
- Counting, summing, filtering
Core Principle (Exam-Safe Rule)
- Loops repeat
- IF statements decide
- Never confuse their roles
Correct Structural Hierarchy (Very Important)
Correct Structure
<initialisation>
<loop>
<selection>
<processing>
</selection>
</loop>
Wrong Structure (Common Mistake)
<selection>
<loop>
<processing>
</loop>
</selection>
- Why wrong
- IF only checks once
- Loop becomes conditionally unreachable
The Three Standard Combination Patterns
| Pattern | Description | Typical Use |
|---|---|---|
| Selection inside Iteration | IF inside loop | Counting, filtering |
| Iteration inside Selection | Loop inside IF | Rare, risky |
| Selection controlling loop exit | IF affects loop condition | Searching |
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
Pattern 1: Selection Inside Iteration (MOST IMPORTANT)
When To Use
- Every item must be processed
- Decision is made for each iteration
Example 1: Counting Values That Meet A Condition
- Scenario
- Input 10 numbers, count how many are positive
count ← 0
FOR i ← 1 TO 10
INPUT num
IF num > 0 THEN
count ← count + 1
ENDIF
NEXT i
OUTPUT count
- Examiner focus
- IF inside loop
- Correct counter update
- Pitfall
- Putting IF outside loop (counts only last input)
Example 2: Summation With Condition
- Scenario
- Add only even numbers
total ← 0
FOR i ← 1 TO 10
INPUT num
IF num MOD 2 = 0 THEN
total ← total + num
ENDIF
NEXT i
OUTPUT total
- Why correct
- Decision applied every iteration
- Pitfall
- Adding outside IF (adds all values)
Example 3: Array Filtering
- Scenario
- Count marks ≥ 50
countPass ← 0
FOR index ← 1 TO n
IF marks[index] >= 50 THEN
countPass ← countPass + 1
ENDIF
NEXT index
Pattern 2: Selection Controlling Iteration (Early Exit Logic)
When To Use
- Loop should stop early when condition is met
- Searching problems
Example 4: Linear Search With WHILE + IF
- Scenario
- Find target value in array
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
- Why correct
- Selection updates flag
- Flag affects loop continuation
- Pitfall
- Forgetting to increment index when NOT found
Example 5: Search Using REPEAT UNTIL
index ← 1
found ← FALSE
REPEAT
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
UNTIL found = TRUE OR index > n
- Important
- Exit condition must protect array bounds
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
Pattern 3: Iteration Inside Selection (Use Carefully)
When It Is Acceptable
- Loop runs only if a condition is true
- Rare in exams
- Must be very clear
Example 6: Conditional Processing
- Scenario
- Only process list if user agrees
INPUT choice
IF choice = "Y" THEN
FOR i ← 1 TO n
OUTPUT data[i]
NEXT i
ENDIF
- Pitfall
- Overusing this pattern
- Examiner preference
- Usually wants loop first, IF inside
Common Exam Applications (Mapped Clearly)
| Task | Loop | Selection |
|---|---|---|
| Count passes | FOR | IF |
| Input validation | REPEAT | IF |
| Search | WHILE | IF |
| Menu | WHILE / REPEAT | IF / ELSE |
| Frequency count | FOR | IF |
| Stop on condition | WHILE | IF |
Nested Selection Inside Iteration (Very Common)
Example 7: Multiple Conditions Per Iteration
- Scenario
- Categorise marks
FOR i ← 1 TO n
INPUT mark
IF mark >= 80 THEN
OUTPUT "A"
ELSEIF mark >= 60 THEN
OUTPUT "B"
ELSE
OUTPUT "Fail"
ENDIF
NEXT i
- Pitfall
- Forgetting ELSEIF order
- Rule
- Highest condition first
Combining With Counters And Accumulators
Example 8: Count And Sum Together
- Scenario
- Average of positive numbers
count ← 0
total ← 0
FOR i ← 1 TO n
INPUT num
IF num > 0 THEN
total ← total + num
count ← count + 1
ENDIF
NEXT i
IF count > 0 THEN
average ← total / count
ENDIF
- Why IF after loop
- Prevents division by zero
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 Examiner Mistakes (High-Frequency)
Mistake 1: IF Outside Loop
FOR i ← 1 TO 10
INPUT num
NEXT i
IF num > 0 THEN
count ← count + 1
ENDIF
- Error
- IF checks only last value
Mistake 2: Missing ELSE Path
- Leads to:
- Counter not updated
- Infinite loops
Mistake 3: Selection That Does Not Affect Loop
- Example
WHILE i <= 10 DO
IF i = 5 THEN
OUTPUT "Five"
ENDIF
i ← i + 1
ENDWHILE
- Safe but pointless IF
- Examiner expects logic relevance
Mistake 4: Incorrect Update Placement
- Example
WHILE i <= 10 DO
IF condition THEN
i ← i + 1
ENDIF
ENDWHILE
- Infinite if condition never true
Dry-Run Check For Combined Logic
- Does loop variable always update?
- Does IF execute every iteration?
- Can loop exit early if needed?
- Are all paths safe?
Safe Combination Templates (Copy-Paste Ready)
Template A: Counting With Condition
count ← 0
FOR i ← 1 TO n
IF <condition> THEN
count ← count + 1
ENDIF
NEXT i
Template B: Search With Early Exit
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
Template C: Validation Loop
REPEAT
INPUT value
UNTIL <valid condition>
Examiner’s Mental Checklist (Use This Before Writing)
- Loop chosen correctly?
- Selection inside loop where needed?
- All variables initialised?
- All paths update control variable?
- Loop guaranteed to terminate?
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
- Repeat with loop, decide with IF
- IF inside loop = per-item logic
- IF controlling loop = early exit
- Wrong nesting = lost marks
- If logic looks clever, simplify
