Selection And Iteration Code Guides: Choosing The Correct Loop For A Problem (Copy)
Choosing The Correct Loop For A Problem
Why Loop Selection Matters
- Choosing the wrong loop:
- Makes logic harder to understand
- Increases risk of infinite loops
- Loses easy method marks even if output is correct
- Examiners expect:
- FOR when repetitions are known
- WHILE when repetitions are unknown and condition checked first
- REPEAT UNTIL when code must run at least once
Core Decision Rule (Exam-Safe Thinking)
- Ask three questions before writing any loop:
- Do I know how many times this must run?
- Must the code run at least once?
- Should the condition be checked before or after execution?
Master Decision Table (Use This First)
| Question | Yes → Use | No → Consider |
|---|---|---|
| Exact number of repetitions known? | FOR | WHILE / REPEAT |
| Must loop run at least once? | REPEAT UNTIL | FOR / WHILE |
| Condition checked before loop? | WHILE | REPEAT UNTIL |
| Fixed range or index traversal? | FOR | WHILE |
| Input validation after input? | REPEAT UNTIL | WHILE |
| Early exit required? | WHILE / FOR with flag | FOR (basic) |
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
FOR Loop: When And Why To Choose It
When FOR Is The Correct Choice
- Number of repetitions is known in advance
- Loop runs through:
- A fixed range (1 to 10)
- All elements of an array using indices
- Loop control is simple and predictable
Typical FOR Scenarios
- “Repeat 20 times”
- “Process all values in an array of size n”
- “Display numbers from 1 to 50”
- “Add marks of 30 students”
Correct FOR Pattern
FOR i ← 1 TO n
<process>
NEXT i
FOR Loop Pitfalls
- Using FOR when repetitions are unknown
- Changing the counter inside the loop
- Wrong start/end bounds
WHILE Loop: When And Why To Choose It
When WHILE Is The Correct Choice
- Number of repetitions is unknown
- Loop depends on:
- User input
- Data conditions
- Condition must be checked before execution
Typical WHILE Scenarios
- “Keep asking until input is valid”
- “Repeat while user has not chosen exit”
- “Search until item found”
- “Repeat while stock > 0”
Correct WHILE Pattern
WHILE <condition> DO
<process>
ENDWHILE
WHILE Loop Pitfalls
- Forgetting to update condition variables
- Condition never becoming FALSE
- Using WHILE when loop must run at least once
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
REPEAT UNTIL Loop: When And Why To Choose It
When REPEAT UNTIL Is The Correct Choice
- Loop must execute at least once
- Input must be taken before validation
- Exit condition is clearer than continuation condition
Typical REPEAT UNTIL Scenarios
- “Enter a number until it is valid”
- “Show menu at least once”
- “Repeat until user chooses to quit”
- “Allow limited attempts”
Correct REPEAT UNTIL Pattern
REPEAT
<process>
UNTIL <condition>
REPEAT UNTIL Pitfalls
- Writing condition like a WHILE loop
- Forgetting that condition stops loop when TRUE
- Infinite loops due to condition never becoming TRUE
Side-By-Side Comparison (Fast Recall Table)
| Feature | FOR | WHILE | REPEAT UNTIL |
|---|---|---|---|
| Repetitions known | Yes | No | No |
| Condition checked | Before | Before | After |
| Minimum runs | Fixed | 0 | 1 |
| Best for arrays | Yes | Possible | Rare |
| Best for validation | No | Yes | Yes |
| Best for menus | No | Yes | Yes |
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
Problem-Based Loop Selection (Exam-Style Scenarios)
Scenario 1: “Input 10 numbers and find the total”
- Correct loop: FOR
- Reason
- Number of inputs is fixed
total ← 0
FOR i ← 1 TO 10
INPUT num
total ← total + num
NEXT i
Scenario 2: “Keep asking until the user enters a positive number”
- Correct loop: REPEAT UNTIL
- Reason
- Input must occur at least once
REPEAT
INPUT num
UNTIL num > 0
Scenario 3: “Repeat while the user does not choose exit”
- Correct loop: WHILE
- Reason
- Condition must be checked before repeating
INPUT choice
WHILE choice <> "Q" DO
<menu actions>
INPUT choice
ENDWHILE
Scenario 4: “Search array and stop when found”
- Correct loop: WHILE
- Reason
- Early exit required
index ← 1
found ← FALSE
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
Scenario 5: “Allow maximum 3 attempts”
- Correct loop: REPEAT UNTIL
- Reason
- Must allow at least one attempt
attempts ← 0
REPEAT
INPUT password
attempts ← attempts + 1
UNTIL password = correct OR attempts = 3
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-Focused Mistakes (And How To Avoid Them)
Mistake 1: Using FOR For Input Validation
- Why wrong
- Number of attempts is unknown
- Correct choice
- WHILE or REPEAT UNTIL
Mistake 2: Using WHILE When Loop Must Run Once
- Why wrong
- Loop may never execute
- Correct choice
- REPEAT UNTIL
Mistake 3: Overcomplicating Simple Repetition
- Example
- Using WHILE instead of FOR for fixed count
- Fix
- Use FOR for clarity and safety
Mistake 4: Writing Clever But Unsafe Logic
- Example
- Multiple conditions without clarity
- Fix
- Use simplest loop that meets requirements
Mental Checklist Before Writing Any Loop
- Can I write the loop bounds clearly? → FOR
- Do I need to test condition first? → WHILE
- Must the loop run at least once? → REPEAT UNTIL
- Do I need early termination? → WHILE / FOR with flag
- Is readability important? → Choose simplest valid loop
Final Loop-Choice Summary (One-Line Rules)
- Fixed number → FOR
- Unknown number → WHILE
- Must run once → REPEAT UNTIL
- Array traversal → FOR
- Validation after input → REPEAT UNTIL
- Early exit → WHILE
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
