Q&A
Using for loops in C
Overview
The for loop is C's primary counting loop, ideal when you know (or can express) how many times to iterate. Its three-part header puts the loop's setup, continuation test, and step in one place, making counting loops compact and readable.
graph TD
A[init: runs once] --> B{condition true?}
B -->|yes| C[execute body]
C --> D[update]
D --> B
B -->|no| E[exit loop]
style B fill:#1e1b4b,color:#fff
Anatomy
for (initialization; condition; update) {
// loop body
}
- initialization - runs once before the loop (e.g.
int i = 0). - condition - checked before each iteration; loop runs while it's true.
- update - runs after each iteration (e.g.
i++).
Basic Examples
// Print 0 through 4
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
// Count down
for (int i = 10; i > 0; i--) {
printf("%d ", i); // 10 9 8 ... 1
}
// Step by 2
for (int i = 0; i <= 10; i += 2) {
printf("%d ", i); // 0 2 4 6 8 10
}
Note: declaring the loop variable inside the for (int i = 0) requires C99 or later; older C89 requires declaring i before the loop.
Iterating an Array
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]); // number of elements
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
The sizeof(arr)/sizeof(arr[0]) idiom computes array length (works only on a real array in scope, not a pointer).
All Parts Are Optional
Any of the three header parts can be omitted:
// Infinite loop (empty condition is always true)
for (;;) {
// ... use break to exit
}
// Initialization done elsewhere, no update in header
int i = 0;
for (; i < 5;) {
printf("%d ", i);
i++; // update inside body
}
Multiple Variables (comma operator)
for (int i = 0, j = 9; i < j; i++, j--) {
printf("%d %d\n", i, j); // converge from both ends
}
The comma operator lets you initialize and update more than one variable.
break and continue
for (int i = 0; i < 10; i++) {
if (i == 5) break; // exit the loop entirely
if (i % 2 == 0) continue; // skip to the next iteration
printf("%d ", i); // prints odd numbers < 5: 1 3
}
break- leave the loop immediately.continue- skip the rest of the body and go to the update/condition.
Nested Loops
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("(%d,%d) ", i, j);
}
printf("\n");
}
Common for grids/matrices; complexity multiplies (here O(n²)).
Why This Matters in Interviews
C interviews use for loops to check fundamentals: the init/condition/update order and when each runs (init once; condition before each iteration; update after), the array-length idiom, that all header parts are optional (infinite loops, for(;;)), the comma operator for multiple variables, and break/continue. A frequent gotcha: the condition is checked before the first body execution, so a false condition means the body never runs.
Common Pitfalls
- Off-by-one errors:
<vs<=changes the iteration count and can overrun arrays. - Declaring
int iinsideforon a C89 compiler (use C99+ or declare beforehand). - Modifying the loop variable inside the body, causing unexpected counts.
- Using
sizeof(arr)/sizeof(arr[0])on a pointer (e.g. an array passed to a function decays to a pointer) - gives the wrong length. - Forgetting the update, creating an infinite loop.
References
- C
forstatement: https://en.cppreference.com/w/c/language/for - C language reference: https://en.cppreference.com/w/c/language