Course Content
Paper 2 – Problem Solving Approach -Practical
0/102
Past Paper-2 May/June 2025
Past Paper-1 May/June 2025
Download Files
0/1
Olevel : Computer Science 2210 / IGCSE 0478 : Recorded

WHILE Loop (Pre-counted Loop – Turns) Explanation – O Level 2210

A WHILE loop is a type of loop used when we want to repeat a set of instructions as long as a condition is true.
It checks the condition before entering the loop — this is why it is called a pre-condition loop or pre-checked loop.

When we use a WHILE loop for a fixed number of turns, we call it a pre-counted loop because the number of repetitions is based on a counter that controls how many turns the loop runs.


How a WHILE Loop Works

  1. The condition is checked first.

  2. If the condition is true, the loop runs.

  3. After running the instructions, the condition is checked again.

  4. The loop stops when the condition becomes false.


Pseudocode Example (Pre-counted Turns)

Example: Display numbers 1 to 5 using a WHILE loop

DECLARE count : INTEGER
count ← 1

WHILE count <= 5
OUTPUT count
count ← count + 1
ENDWHILE


Explanation of Example

  • The counter count starts at 1

  • Before each turn, we check:
    count <= 5

  • If it’s true, the loop runs

  • After printing the number, the counter is increased by 1

  • When count becomes 6, the condition becomes false and the loop stops

This loop runs 5 turns in total.


Why Use a WHILE Loop? (Benefits)

1. Condition is checked before running

Useful when you want the loop to run only if a given condition is true.

2. Flexible

You can stop the loop based on:

  • Counter

  • User input

  • Sensor values

  • Calculations

3. Good for unknown or changing repetitions

While a FOR loop is ideal when repetitions are fixed,
a WHILE loop is better when repetitions depend on a condition.

4. Can also be used as a pre-counted loop

By using a counter (like count ← count + 1),
you can run it for a specific number of turns — just like a FOR loop.


Student-Friendly Summary

A WHILE loop repeats instructions as long as its condition is true.
Because the condition is checked before running the loop, it is called a pre-condition loop.
When used with a counter, it becomes a pre-counted loop that runs for a fixed number of turns.

You cannot copy content of this page