Selection And Iteration Code Guides: WHILE Loops: Entry-Controlled Iteration (Copy)
WHILE Loops: Entry-Controlled Iteration
What A WHILE Loop Is (Entry-Controlled Loop)
- A WHILE loop repeats a block of code as long as a condition is TRUE
- The condition is checked before the loop body runs
- If the condition is FALSE at the start:
- The loop does not run at all
- Best used when:
- The number of repetitions is unknown
- The loop depends on user input, data validity, or dynamic conditions
Standard WHILE Loop Structure (Pseudocode)
WHILE <condition> DO
<statements>
ENDWHILE
- Execution order
- Check condition
- If TRUE → run body
- Update variables
- Re-check condition
- Critical rule
- The condition must eventually become FALSE or the loop becomes infinite
WHILE Loop Vs FOR Loop (Quick Decision Table)
| Situation | Use WHILE | Use FOR |
|---|---|---|
| Number of repetitions unknown | Yes | No |
| Input validation | Yes | No |
| Repeat until user chooses to stop | Yes | No |
| Fixed number of repetitions | No | Yes |
| Traversing array with known size | Possible but not ideal | 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
Structure Components (What Each Part Does)
Condition
- A Boolean expression
- Must evaluate to TRUE or FALSE
- Examples
num < 0choice <> "Q"count <= 10
- Examiner trap
- Writing a condition that never changes
Loop Body
- Code that runs repeatedly
- Must include:
- Input
- Processing
- Update to a variable used in the condition
Update Statement (Exit Control)
- Changes a variable involved in the condition
- Without it → infinite loop
Core WHILE Loop Patterns (High-Frequency Exam Use)
Pattern 1: Input Validation
- Scenario
- Keep asking user until they enter a positive number
INPUT num
WHILE num <= 0 DO
OUTPUT "Invalid input"
INPUT num
ENDWHILE
- Why WHILE fits
- Condition checked before accepting input
- Pitfall
- Forgetting second
INPUT numinside loop
- Forgetting second
Pattern 2: Unknown Number Of Inputs (Sentinel-Controlled)
- Scenario
- Input numbers until user enters
-1, then output total
- Input numbers until user enters
total ← 0
INPUT num
WHILE num <> -1 DO
total ← total + num
INPUT num
ENDWHILE
OUTPUT total
- Key idea
-1is the sentinel value
- Pitfall
- Adding sentinel to total (logic order mistake)
Pattern 3: Menu-Controlled Program
- Scenario
- Keep showing menu until user chooses exit option
INPUT choice
WHILE choice <> "X" DO
IF choice = "A" THEN
OUTPUT "Option A selected"
ELSE
OUTPUT "Invalid choice"
ENDIF
INPUT choice
ENDWHILE
- Examiner focus
- Loop condition
- Correct re-input placement
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
WHILE Loop With Counters (Controlled Exit)
Pattern 4: Count Up To A Limit
- Scenario
- Output numbers from 1 to 5 using WHILE
i ← 1
WHILE i <= 5 DO
OUTPUT i
i ← i + 1
ENDWHILE
- Difference from FOR
- Counter update is manual
- Pitfall
- Forgetting
i ← i + 1
- Forgetting
Pattern 5: Count Down
- Scenario
- Output numbers from 5 down to 1
i ← 5
WHILE i >= 1 DO
OUTPUT i
i ← i - 1
ENDWHILE
- Pitfall
- Wrong condition (
i <= 1) causes zero iterations
- Wrong condition (
Using WHILE With Arrays
Pattern 6: Traverse Array Until Condition Met
- Scenario
- Find first value greater than 50 in array
arr[1..n]
- Find first value greater than 50 in array
index ← 1
WHILE index <= n AND arr[index] <= 50 DO
index ← index + 1
ENDWHILE
IF index <= n THEN
OUTPUT arr[index]
ELSE
OUTPUT "Not found"
ENDIF
- Why WHILE is ideal
- Loop stops early once condition is met
- Pitfall
- Accessing
arr[index]whenindex > n
- Accessing
Pattern 7: Searching With Flag Variable
- Scenario
- Search for value
targetin array
- Search for value
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
IF arr[index] = target THEN
found ← TRUE
ELSE
index ← index + 1
ENDIF
ENDWHILE
IF found = TRUE THEN
OUTPUT "Found"
ELSE
OUTPUT "Not found"
ENDIF
- Examiner likes
- Correct flag usage
- Safe condition ordering
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
Tracing A WHILE Loop (Step-By-Step Method)
Tracing Checklist
- Write columns for:
- Condition result (TRUE/FALSE)
- Control variable value
- Output
- Check condition before entering loop
- Update variable at correct point
Worked Trace Example
i ← 1
WHILE i < 4 DO
OUTPUT i
i ← i + 1
ENDWHILE
| Step | i | Condition (i < 4) | Output |
|---|---|---|---|
| Start | 1 | TRUE | — |
| Loop 1 | 1 | TRUE | 1 |
| Loop 2 | 2 | TRUE | 2 |
| Loop 3 | 3 | TRUE | 3 |
| End | 4 | FALSE | — |
Common Examiner Pitfalls (And How To Avoid Them)
Pitfall 1: Infinite Loop
- Cause
- Condition never becomes FALSE
- Example
i ← 1
WHILE i <= 5 DO
OUTPUT i
ENDWHILE
- Fix
- Add
i ← i + 1
- Add
Pitfall 2: Condition Never True
- Example
i ← 10
WHILE i < 5 DO
OUTPUT i
i ← i + 1
ENDWHILE
- Result
- Loop never runs
- Fix
- Correct initial value or condition
Pitfall 3: Wrong Condition Direction
- Example
- Using
=instead of<or<>
- Using
- Fix
- Re-read condition in plain English:
- “Repeat while …”
- Re-read condition in plain English:
Pitfall 4: Forgetting Initial Input
- Example (sentinel loop)
WHILE num <> -1 DO
INPUT num
ENDWHILE
- Problem
numnot initialised
- Fix
- Always input once before 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
Comparing Entry-Controlled And Exit-Controlled Loops
| Feature | WHILE Loop | REPEAT UNTIL Loop |
|---|---|---|
| Condition checked | Before loop | After loop |
| Minimum runs | 0 | 1 |
| Input validation | Good | Risky |
| Sentinel loops | Very suitable | Less common |
Copy-Paste WHILE Loop Templates (Exam-Safe)
Template A: Input Validation
INPUT value
WHILE value < 0 DO
INPUT value
ENDWHILE
Template B: Sentinel-Controlled Total
total ← 0
INPUT num
WHILE num <> -1 DO
total ← total + num
INPUT num
ENDWHILE
OUTPUT total
Template C: Count-Controlled WHILE
i ← 1
WHILE i <= n DO
<process>
i ← i + 1
ENDWHILE
Template D: Search With Early Exit
found ← FALSE
index ← 1
WHILE index <= n AND found = FALSE DO
<check>
ENDWHILE
“Spot The Bug” Mini Practice
Bug 1
WHILE count < 5 DO
OUTPUT count
ENDWHILE
- Error
countnever changes
- Fix
- Add
count ← count + 1
- Add
Bug 2
WHILE num <> -1 DO
total ← total + num
INPUT num
ENDWHILE
- Error
numnot initialised
- Fix
- Add
INPUT numbefore loop
- Add
Bug 3
i ← 5
WHILE i <= 1 DO
OUTPUT i
i ← i - 1
ENDWHILE
- Error
- Condition wrong
- Fix
WHILE i >= 1 DO
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
