Loops in C
October 6, 2026 • 5 min read

Table of contents
Part of the series:C Explained
A loop runs the same block of code repeatedly, as long as a condition stays true. It is the feature that makes programs useful, because most real work is repeating an operation a certain number of times or until a goal is reached. C has three loops: while, do/while, and for. All three run a block; they differ only in when the condition is checked.
while
The while loop checks its condition before each run. If the condition is false at the start, the body never runs:
int n = 3;
while (n > 0) {
printf("%d\n", n);
n = n - 1;
}
3
2
1
The same truthiness rule from conditionals applies: any non-zero value loops, zero stops.
The infinite loop and how to escape
If the condition never becomes false, the loop never ends. That is the infinite loop. Sometimes it is intentional and broken with break; usually it is a bug. The most common causes: forgetting to update the counter, and comparing with = instead of ==.
int n = 3;
while (n > 0) { // bug: n never changes
printf("%d\n", n);
}
This prints 3 forever and never terminates. If your program hangs, look for a loop whose condition never flips. break exits the innermost loop immediately; continue skips straight to the next condition check:
int i = 0;
while (1) { // intentional infinite loop
i++;
if (i == 3) continue; // skip printing 3
if (i > 5) break; // leave the loop at 6
printf("%d\n", i);
}
1
2
4
5
A while (1) or for (;;) loop paired with break is the standard way to write “loop forever until something special happens”, common when reading input until the end is reached.
do / while
The do/while loop runs the body first and checks the condition after. It always executes at least once:
int n = 0;
do {
printf("n = %d\n", n);
n++;
} while (n < 3);
n = 0
n = 1
n = 2
The body runs with n = 0, then tests 0 < 3, runs again, and stops when n is 3. Use do/while when the body must run once before any condition makes sense, like asking for input at least once, or processing one menu choice before checking whether to continue. Note the ; after the while (n < 3) line; a do/while is the one loop that ends with a semicolon.
for
The for loop packs counting into one line: an initialization, a condition, and an update, separated by semicolons:
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
0
1
2
3
4
Reading order matters: int i = 0 runs once before the loop; the condition i < 5 is checked before each run, like a while; i++ runs at the end of each iteration before the next check. The same loop in while form shows the relationship:
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
Use for when you know the number of iterations, because the count is visible in one line: start, stop, step. It is the natural tool for iterating over arrays, which the arrays post will use constantly.
The three parts are individually optional. for (;;) is an infinite loop. You can declare the counter inside the heading (int i = 0), in which case it only exists inside the loop, or declare it before and let the loop update it:
int i;
for (i = 0; i < 10; i++) {
// ...
}
// i is still usable here, holding 10
Count down and step by more than one
The update step is any expression, so the loop is flexible:
for (int i = 10; i > 0; i--) { ... } // 10, 9, ..., 1
for (int i = 0; i < 10; i += 2) { ... } // 0, 2, 4, 6, 8
Nested loops
A loop body can contain another loop. Nested loops run “all of the inner loop for each step of the outer loop”:
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
printf("%d%d ", row, col);
}
printf("\n");
}
11 12 13
21 22 23
31 32 33
The inner loop completes fully before the outer loop advances. This is the shape you will use later to walk rows and columns of matrices.
Accumulating a result
A loop plus a variable that changes each iteration is the workhorse of programming. The accumulator starts at a neutral value and absorbs each iteration:
int total = 0;
for (int i = 1; i <= 10; i++) {
total += i;
}
printf("sum of 1..10 = %d\n", total); // 55
Zero for sums, one for products. Counters, sums, and best-so-far values all follow this pattern.
The big picture
while checks before running, do/while checks after running, and for collects the count into one heading for the common “start at A, go to B, step by C” case. break leaves a loop early, continue skips one iteration, and infinite loops are either intentional (with break) or a bug from a condition that never flips. With conditionals and loops you can now control any program; the next post packages code into reusable units: functions.
/sponsor
Enjoyed this post? You can sponsor me and this site.