Loops are one of the first control-flow tools that make C feel like a real programming language instead of a calculator with extra steps. They let you repeat work without copying the same code block over and over, which is important whether you are printing a menu, stepping through an array, or building a simple game loop. Once you understand for, while, and do...while, you can turn many repetitive tasks into compact, readable code.
A good way to learn loops in C is to think about three things at the same time:
- What starts the loop.
- What keeps it running.
- What stops it.
If those three parts are clear, the syntax becomes much easier to remember.
What a loop does in C
A loop repeats a block of code while a condition stays true, or for a fixed number of iterations. C gives you three common loop forms:
forloop: best when you know how many times you want to repeat.whileloop: best when you want to keep going until a condition changes.do...whileloop: best when the code must run at least once.
All three are built from the same basic idea. The difference is mostly about where the condition is checked and how explicitly you express the repetition logic.
| Loop type | Condition check | Best for | Runs at least once? |
|---|---|---|---|
for | Before each iteration | Counted repetition | No |
while | Before each iteration | Unknown number of repeats | No |
do...while | After each iteration | Menu prompts, input loops | Yes |
The for loop
The for loop is the most structured loop in C. It usually includes three pieces in one line: initialization, condition, and update.
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
This example starts i at 0, keeps looping while i < 5, and increases i by one after each pass. The result is a sequence from 0 to 4.
Here is how to read it:
- Start with
i = 0. - As long as
i < 5, keep repeating. - After each repetition, add 1 to
i.
That pattern makes for loops ideal for array traversal, counting, and any task where the number of iterations is known in advance.
Common for loop mistakes
A few errors show up again and again:
- Off-by-one conditions, such as using
i <= 5when you meanti < 5. - Forgetting to update the counter, which can create an infinite loop.
- Using the wrong starting value when indexing arrays.
If you are looping over an array of 10 elements, the valid indexes are 0 through 9, not 1 through 10.
The while loop
A while loop checks its condition before each iteration. That means if the condition is false at the beginning, the loop body never runs.
int count = 0;
while (count < 3) {
printf("count = %d\n", count);
count++;
}
This code prints three lines and then stops. The key difference from for is style: the setup and update happen outside the loop header.
Use while when the repetition depends on a condition that may change unpredictably, like:
- Waiting for valid input.
- Reading until end-of-file.
- Repeating until the user types
quit.
A while loop is especially useful when the loop body itself influences whether the loop should continue.
When while is clearer than for
Even if a for loop could technically do the job, while may be easier to read when the loop is condition-driven rather than count-driven. For example, when you are processing input until a sentinel value appears, the condition is the real story, not the number of passes.
The do...while loop
A do...while loop is similar to while, but the body runs first and the condition is checked afterward.
int choice;
do {
printf("Enter 1 to continue or 0 to stop: ");
scanf("%d", &choice);
} while (choice != 0);
This is useful for menus because the user should see the prompt before the condition is evaluated. Since the body executes before the condition check, the loop runs at least once.
That guarantee is the main reason to use do...while. If you need the code to display something, gather input, or perform an action before deciding whether to repeat, it is the right tool.
Loop control statements
C also gives you statements that alter the normal loop flow.
break
break exits the loop immediately.
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
This stops the loop when i reaches 5.
continue
continue skips the rest of the current iteration and moves to the next one.
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
printf("%d\n", i);
}
This prints 0, 1, 3, and 4, skipping 2.
Use break when the loop should end early. Use continue when one specific iteration should be skipped but the loop should keep going.
Choosing the right loop
A practical way to decide is to ask what kind of repetition you need.
- Use
forwhen the loop count is known or naturally counted. - Use
whilewhen the loop depends on a condition that changes over time. - Use
do...whilewhen the body must run before the condition is checked.
That simple decision tree covers most beginner and intermediate C code.
Looping through an array
One of the most common uses of loops in C is processing arrays.
int main() {
int nums[] = {10, 20, 30, 40};
int size = sizeof(nums) / sizeof(nums[0]);
for (int i = 0; i < size; i++) {
printf("nums[%d] = %d\n", i, nums[i]);
}
return 0;
}
This example is worth studying closely because it combines several essential ideas:
- The array stores multiple values.
sizeis computed from the array length.- The
forloop visits each element by index. - The loop condition uses
< size, which avoids stepping past the end.
That pattern appears constantly in C programs, so it pays to memorize it.
Nested loops
Sometimes one loop is not enough. If you need to handle rows and columns, tables, or repeated patterns, you use nested loops: a loop inside another loop.
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 4; col++) {
printf("(%d,%d) ", row, col);
}
printf("\n");
}
The outer loop controls the rows. The inner loop controls the columns. Every time the outer loop advances, the inner loop restarts from the beginning.
Nested loops are powerful, but they can also get hard to read. If the logic starts to feel tangled, consider whether a helper function would make the code clearer.
Common loop problems and fixes
Here are a few issues to watch for when you write loops in C:
- Infinite loop: the condition never becomes false, usually because the update step is missing or wrong.
- Off-by-one error: the loop runs one time too many or too few.
- Wrong comparison operator:
<=and<are not interchangeable. - Array overflow: the loop index goes beyond the last valid element.
- Unclear loop purpose: the code works, but no one can easily tell why the loop exists.
A good habit is to read your loop out loud in plain English before you trust it in code.
A simple debugging checklist
When a loop misbehaves, check these items in order:
- Is the condition correct?
- Does the counter or state variable change?
- Is the update happening in the right place?
- Are you using the right start and end values?
- If the loop nests another loop, is the inner logic resetting as expected?
That checklist catches most beginner mistakes quickly.
Practice ideas
If you want to build confidence, try these short exercises:
- Print numbers from 1 to 10 using a
forloop. - Use a
whileloop to keep asking for a positive number. - Write a
do...whilemenu that repeats until the user exits. - Sum the values in an integer array.
- Print a multiplication table with nested loops.
The goal is not just to memorize syntax. It is to recognize which loop shape matches which problem.
Final takeaway
Loops are the foundation of repetition in C. Once you understand how for, while, and do...while differ, you can write cleaner code, reduce duplication, and handle real programs more confidently. Start with one loop at a time, trace it on paper if needed, and always check the start, condition, and update.
If you can explain those three pieces clearly, you already understand the core of C loops.