A loop repeats a block of code until a condition becomes false or you exit with break. Without loops, C programs could not walk arrays, print patterns, validate input, or run menus without copy-pasting the same lines.
Three loop forms
C gives you exactly three looping statements: for, while, and do-while. Any of them can express the same logic, but each shape makes certain programs clearer.
What should beginners learn first?
Start with for for counted work (for (int i = 0; i < n; i++)). Switch to while when repetition depends on input or an external condition. Use do-while when the body must run at least once (menus, validation prompts).
Hub vs single-loop pages
This page teaches how loops work in a C tutorial. Each name in the table links to a focused guide with syntax, examples, and FAQs. Nested loops and break / continue appear in the concepts and examples below.
Prefer clarity over cleverness.
If a counter and stop rule belong together, use for. If you wait on input, use while. If the body must run once first, use do-while.
Infinite loops.
If the condition never becomes false (or you forget to update the counter), the loop runs forever. Always check that something moves toward the stop condition.
C99 loop variables.
You can declare int i inside for (...) with -std=c99 or newer. In older C, declare i before the loop.
Syntax
C Syntax
All three loop forms side by side:
C
/* for — init, test, update in header */
for (int i = 0; i < n; i++) {
/* body */
}
/* while — test before body */
while (condition) {
/* body */
}
/* do-while — body first, then test; note semicolon */
do {
/* body */
} while (condition);
Pick 1, 2, or 3: 9
Pick 1, 2, or 3: 2
You picked 2
How It Works
Body first shows the prompt before any test.
scanf reads the choice.
while repeats while the value is out of range.
Trailing semicolon after while (...) is required.
Example 4 — Nested for multiplication table
Outer loop = rows, inner loop = columns.
C
#include <stdio.h>
int main(void) {
int row, col;
for (row = 1; row <= 3; row++) {
for (col = 1; col <= 3; col++) {
printf("%d ", row * col);
}
printf("\n");
}
return 0;
}
Output
1 2 3
2 4 6
3 6 9
How It Works
Outer for picks the row.
Inner for prints every column for that row.
3 × 3 = 9 body runs for the product cells.
Star / alphabet patterns use the same nesting idea.
Example 5 — Print 0, 1, 2 three ways
Same result with for, while, and do-while.
C
#include <stdio.h>
int main(void) {
int i;
printf("for: ");
for (i = 0; i < 3; i++) {
printf("%d ", i);
}
printf("\n");
printf("while: ");
i = 0;
while (i < 3) {
printf("%d ", i);
i++;
}
printf("\n");
printf("do-while: ");
i = 0;
do {
printf("%d ", i);
i++;
} while (i < 3);
printf("\n");
return 0;
}
Output
for: 0 1 2
while: 0 1 2
do-while: 0 1 2
How It Works
Same counting job can use any of the three forms.
for packs init/test/update together.
while / do-while keep update in the body.
Pick the form that makes your intent clearest.
Index
C Loops Table
Loop guides in this C tutorial track. Open any row for a full guide with syntax, examples, and FAQs (3 tutorials).
Counted & pre-test loops
2 tutorials
Repeat with a clear counter, or test the condition before each pass.
One line: C loops repeat work—use for for counts, while for conditions, do-while when the body must run once, then open each loop guide for details.
Frequently Asked Questions
Loops repeat a block of code while a condition holds or for a set number of iterations. C provides for, while, and do-while. They power counting, array walks, menus, input validation, and pattern printing.
for bundles init, test, and update in one header—best for counted loops. while tests before the body—the body may run zero times. do-while tests after the body—it always runs at least once.
Use for when you know the iteration pattern upfront (index from 0 to n-1). Use while when repetition depends on runtime input or an external condition (read until EOF, menu until quit).
Use do-while when the body must run at least once before you can test the stop condition—input validation prompts, menus that must display once, play-again dialogs.
break leaves the innermost loop immediately. continue skips the rest of the current iteration and jumps to the next condition test. Both work inside for, while, and do-while.
Read the comparison table and examples on this page, then open the for loop tutorial first if you are new to C loops. Follow the sidebar order: for, while, do-while.