Loops in C
Imagine printing "Hello" 100 times. Writing 100 printf lines would be silly. A loop repeats code for you: write it once, run it as many times as you need.
C has three loops: for, while and do-while. This lesson gives you the big picture: what every loop needs, the difference between entry-controlled and exit-controlled loops, and the same program written with all three.
What You Will Learn
- What a loop is and why we use it.
- The 4 parts of every loop.
- The 3 types of loops in C.
- Entry-controlled vs exit-controlled loops.
- Which loop to choose when.
Loops in C Explained
Exam definition: A loop is a control statement that executes a block of code repeatedly as long as a condition is true. C has three loops: for, while and do-while. Each repetition is called an iteration.
The 4 parts of every loop
| Part | Job | Example |
|---|---|---|
| 1. Initialization | Give the loop variable its starting value | i = 1 |
| 2. Condition | Decide if the loop should run again | i <= 5 |
| 3. Body | The work to repeat | printf("%d ", i); |
| 4. Update | Change the loop variable, moving towards the end | i++ |
Memory trick: "I C B U" = "I Can Be Unstoppable": Initialize, Condition, Body, Update. Forget the Update and your loop really becomes unstoppable (infinite)!
Types of loops
| Loop | Condition checked | Type | Runs at least once? | Best when |
|---|---|---|---|---|
for | Before each iteration | Entry-controlled | No | You know how many times (count-based) |
while | Before each iteration | Entry-controlled | No | You don't know how many times (condition-based) |
do-while | After each iteration | Exit-controlled | Yes | The body must run at least once (menus, input checks) |
Entry-controlled vs exit-controlled
- Entry-controlled (for, while): the condition is checked at the entry gate. If it is false at the start, the body never runs.
- Exit-controlled (do-while): the condition is checked at the exit gate, after the body. So the body always runs at least once.
Syntax of the Three Loops
for (init; condition; update) init; init;
{ while (condition) do
body; { {
} body; body;
update; update;
} } while (condition);
Note the semicolon after while (condition) in a do-while. It is required there, and only there.
Example 1: Print 1 to 5 with All Three Loops
#include <stdio.h>
int main(void)
{
int i;
printf("for : ");
for (i = 1; i <= 5; i++)
printf("%d ", i);
printf("\nwhile : ");
i = 1;
while (i <= 5)
{
printf("%d ", i);
i++;
}
printf("\ndo-while: ");
i = 1;
do
{
printf("%d ", i);
i++;
} while (i <= 5);
printf("\n");
return 0;
}
Output
for : 1 2 3 4 5 while : 1 2 3 4 5 do-while: 1 2 3 4 5
Code Explanation
- for: all three control parts (start, condition, update) are on one line:
i = 1; i <= 5; i++. - while:
i = 1is before the loop, the condition is in brackets, andi++is inside the body. - do-while: the body runs first, then
while (i <= 5);decides whether to repeat. - All three print 1 to 5, because the condition is true at the start. The difference appears when the condition is false from the beginning (see Example 2).
How a Loop Works (Iteration by Iteration)
- Initialize the loop variable (i = 1).
- Check the condition (1 ≤ 5? yes).
- Run the body (print 1).
- Update (i becomes 2).
- Go back to step 2. Repeat until the condition becomes false (i = 6), then leave the loop.
Example 2: When the Condition Is False from the Start
#include <stdio.h>
int main(void)
{
int n = 10;
while (n < 5)
printf("while body ran\n"); /* never runs */
do
printf("do-while body ran once\n");
while (n < 5);
int sum = 0;
for (int k = 1; k <= 100; k++)
sum += k;
printf("Sum of 1 to 100 = %d\n", sum);
return 0;
}
Output
do-while body ran once Sum of 1 to 100 = 5050
10 < 5 is false. The while body is skipped completely, but the do-while body runs once before checking. The last loop adds 1 + 2 + ... + 100 = 5050.
Important Points to Remember
- A loop repeats code while a condition is true.
- 4 parts: Initialization, Condition, Body, Update.
- 3 loops: for, while, do-while.
- for and while are entry-controlled; do-while is exit-controlled.
- do-while always runs at least once.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Forgetting the update (i++) | Infinite loop | Always move the loop variable towards the end |
for (i = 1; i <= 5; i++); | Empty loop; the body runs once after it | No semicolon after for(...) |
Missing ; after do-while's while(...) | Syntax error | } while (cond); |
Using < instead of <= | Runs one time less (off-by-one) | Check the first and last values |
Loops in C Practice Questions
- What is a loop? Why do we use loops?
- Name the three loops in C.
- Write the four parts of a loop.
- What is the difference between entry-controlled and exit-controlled loops?
- Which loop runs at least once even if the condition is false?
Show answers
- A statement that repeats a block of code while a condition is true. It avoids writing the same code many times.
- for, while, do-while.
- Initialization, condition, body, update.
- Entry-controlled checks the condition before the body (may run 0 times); exit-controlled checks after the body (runs at least once).
- do-while.
Coding Exercise
Print all even numbers from 2 to 20 on one line using any loop.
Show solution
#include <stdio.h>
int main(void)
{
for (int i = 2; i <= 20; i += 2)
printf("%d ", i);
printf("\n");
return 0;
}
2 4 6 8 10 12 14 16 18 20
Quick Quiz: Loops in C (MCQ)
-
Which loop is exit-controlled?
A) for B) while C) do-while D) None
-
Which part of a loop prevents it from running forever?
A) Initialization B) Update C) printf D) Comment
-
How many times does
for (i = 0; i < 5; i++)run?A) 4 B) 5 C) 6 D) Infinite
-
If the condition is false at the start, a while loop runs:
A) 0 times B) 1 time C) Forever D) Error
-
Each single run of a loop body is called:
A) A recursion B) An iteration C) A jump D) A function
Show answers
- C: do-while.
- B: the update.
- B: 5 times (0, 1, 2, 3, 4).
- A: 0 times.
- B: an iteration.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Loop | Repeats code while a condition is true |
|---|---|
| Types | for, while, do-while |
| 4 parts | Initialization, Condition, Body, Update |
| Entry-controlled | for, while (may run 0 times) |
| Exit-controlled | do-while (runs at least once) |
| One run of the body | Iteration |
| Sum 1 to 100 | 5050 |
Frequently asked questions
- What is the difference between entry-controlled and exit-controlled loops?
- Entry-controlled checks the condition before the body; exit-controlled checks it after, so it runs at least once.
- When should you use a for loop?
- When you know the number of iterations in advance.
- When should you use a while loop?
- When you don't know how many times, only the condition to stop.
- What happens if the update is missing?
- The condition never becomes false, so you get an infinite loop.
Remember
- Loop parts: I-C-B-U.
- do-while = at least once.
Next Topic
Next lesson: for Loop in C. You will master the for loop with a step-by-step dry run.