Nearby lessons
43 of 124C - Loops Overview
Loops Overview is one of the foundational topics in C programming. This lesson explains Why Loops? and while vs do-while — Comparison with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Why Loops?
If you want to print numbers 1 to 100, you do NOT write 100 printf lines. A loop repeats a block of code as many times as you want. C has three loops: while, do-while, and for.
In simple words: a loop is a repeated block — one copy of the code runs again and again until a condition stops it. The loop decides when to stop, not you writing more lines.
while vs do-while — Comparison
| Point | while | do-while |
|---|---|---|
| Checks the condition | Before the body | After the body |
| Body runs at least once? | No (may run 0 times) | Yes (always at least once) |
| Use when | You are not sure if it should run | It must run at least once |
📝 Key Takeaways
- while checks first; do-while runs at least once; for gathers start/condition/update.
- for is best when you know the number of iterations.
- break stops the loop; continue skips one round.
- Nested loops: inner loop completes for each outer round.
- Pattern programs = outer loop (rows) + inner loop (columns).
- Never forget the update (i++) — else you get an infinite loop.
🧠 Test Your Knowledge
1 QuestionsProgress: 0 / 1