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:

  1. while — Starts the loop.
  2. (condition) — A question that must be true for the loop to run.

    • If true, repeat the code.
    • If false, stop the loop.
  3. {} — 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


<
Previous Post
🖼️ Canvas vs. SVG: What’s the Difference?
>
Next Post
🔢 Why We Need Two Zeros: +0 and -0