For Loops in C: Syntax, Examples and Common Mistakes
The for loop is one of the first tools you reach for in C. It runs a block of
code a known number of times, which makes it perfect for counting, iterating
over arrays and building tables.
The three parts of a for loop
A C for loop has three sections inside the parentheses, separated by
semicolons:
for (initialization; condition; update) {
// body runs while condition is true
}
- Initialization runs once, before the loop starts. It usually sets a
counter, like
int i = 0. - Condition is checked before every pass. While it is true, the body runs.
- Update runs at the end of every pass, usually to change the counter.
A first example
This loop prints the numbers 1 through 5:
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
Read it as: start i at 1, keep going while i is less than or equal to 5,
and add 1 to i after each pass. The output is the numbers 1 to 5 on separate
lines.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingCounting down
You are not limited to counting up. Swap the condition and the update to count down instead:
for (int i = 5; i >= 1; i--) {
printf("%d ", i);
}
// prints: 5 4 3 2 1
Common mistakes beginners make
A few errors come up again and again when you are starting out:
- The stray semicolon. Writing
for (int i = 0; i < 5; i++);ends the loop with an empty body. The block after it then runs only once. Watch that semicolon right after the closing parenthesis. - Off-by-one errors.
i < 5runs five times (0 to 4);i <= 5runs six times (0 to 5). Decide whether your last value should be included and pick<or<=to match. - Changing the counter inside the body. Modifying
iboth in the update section and inside the loop body makes the loop hard to follow. Keep counter changes in one place.
When to use a for loop
Reach for a for loop when you know, or can compute, how many times you need to
repeat something — printing a multiplication table, summing the elements of an
array, or drawing a pattern. When the number of repetitions depends on a
condition you cannot count in advance, a while loop is usually clearer.
Practising loops by hand is the fastest way to build intuition. Try rewriting the count-up example as a count-down, then as a loop that prints only even numbers. Small variations like these teach you how the three parts work together.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingInfoplanet
Coding, AI & Robotics
Infoplanet is a coding, AI and robotics education center teaching students of all ages since 2001.
Related guides
Arrays in C
A beginner guide to arrays in C — declaring, initializing and indexing them, looping over elements, multidimensional arrays, and avoiding out-of-bounds errors.
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
