🔄 The while Loop in C — Explained Like You’re 10
Imagine saying:
“Keep doing this until I say stop.”
That’s exactly what a while
loop does in C!
🧠What’s a while
Loop?
It repeats code as long as a condition is true.
while (condition) {
// Code to repeat
}
🧩 Let’s Break It Down:
while
— Starts the loop.-
(condition)
— A question that must be true for the loop to run.- If true, repeat the code.
- If false, stop the loop.
{}
— Holds the block of code that repeats.
đź§Ş Example: Print 1 to 5
int number = 1;
while (number <= 5) {
printf("%d\n", number);
number++;
}
💬 What’s Happening?
- Start with
number = 1
. -
The loop checks: Is
number <= 5
?- Yes ➤ print it, add 1, repeat.
- When
number
becomes 6 ➤ condition is false, so it stops.
⚠️ Watch Out: Infinite Loops!
If the condition never becomes false, the loop runs forever. Always make sure something inside the loop changes the condition!
📝 Quick Quiz
Q1:
What is the main purpose of a while
loop?
(A) Define a function (B) Repeat instructions while a condition is true (C) Declare variables (D) Print to the console
Q2:
In the example above, what happens when number
becomes 6?
(A) Loop continues forever
(B) Loop prints 6 and stops
(C) Loop stops — number <= 5
is false
(D) Program crashes