C in 100 Seconds: For Loops | Episode 8
Video: C in 100 Seconds: For Loops — Init Condition Step | Episode 8 by Taught by Celeste AI - AI Coding Coach
C For Loops: Init, Condition, Step
for (int i = 1; i <= 5; i++) { ... }. Three parts in one line: initialise once, check before each iteration, step after each iteration. The clearest way to write a counter loop.
for is the canonical "do this N times" loop in C. The structure puts all the iteration logic at the top so the body stays focused.
The basic shape
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
Output: 1 2 3 4 5.
Three parts in the (...):
int i = 1— initialiser. Runs once before the loop starts.i <= 5— condition. Checked before each iteration. Loop continues while true.i++— step. Runs after each iteration.
Order: init → check → body → step → check → body → step → ... → check (false) → exit.
Equivalent while
The for loop above is exactly equivalent to:
{
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
}
for is just a more compact while with the loop variable at the top. Easier to scan: "i goes from 1 to 5."
Nested for loops
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
printf("(%d,%d) ", row, col);
}
printf("\n");
}
Output:
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
Standard 2D grid traversal. Outer loop walks rows; inner walks columns. Each row prints a newline at the end.
Counting down
for (int i = 10; i > 0; i--) {
printf("%d ", i);
}
// 10 9 8 7 6 5 4 3 2 1
Decrement step i--. Condition i > 0 (or i >= 1).
Stepping by N
for (int i = 0; i <= 100; i += 10) {
printf("%d ", i);
}
// 0 10 20 30 ... 100
i += 10 instead of i++. Useful for "every 10th" or "doubling": i *= 2.
C99: declaring i in the loop
for (int i = 0; i < 5; i++) { ... }
Pre-C99, you had to declare i outside the loop:
int i;
for (i = 0; i < 5; i++) { ... }
Modern C lets you declare inside. The variable is scoped to the loop — it doesn't exist after the loop ends. Cleaner and safer.
Empty parts
Any of the three parts can be empty:
for (;;) { // forever loop (no init, no check, no step)
// ...
if (done) break;
}
int i = 0;
for (; i < 5; i++) { ... } // init done outside
for (int i = 0; i < 5; ) { // step inside body
// ...
i += step;
}
for (;;) is the "loop forever" idiom — equivalent to while (1) but slightly more common in some C styles.
Multiple init or step expressions
for (int i = 0, j = 10; i < j; i++, j--) {
printf("i=%d j=%d\n", i, j);
}
Comma operator lets you chain expressions in init or step. Useful when two variables advance together.
break and continue
Same as in while:
for (int i = 0; i < 100; i++) {
if (i % 7 == 0 && i > 0) {
printf("Found %d\n", i);
break; // exit on first multiple of 7 > 0
}
}
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) continue; // skip evens
printf("%d\n", i); // 1 3 5 7 9
}
break exits the loop. continue jumps to the step expression, then the condition check.
Summing
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
printf("Sum: %d\n", sum); // 5050
The classic "sum 1 to 100" is n*(n+1)/2 = 5050 — closed form. The loop teaches the pattern; closed forms are faster when they exist.
Walking an array
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
sizeof(arr) / sizeof(arr[0]) is the number of elements. Works for arrays declared in the same scope (where sizeof knows the full size). Doesn't work for arrays passed to functions — those decay to pointers.
We dive into arrays properly in episode 10.
Range-based for? Not in C
C++ has for (int x : arr). C does not. If you want to iterate without an index, you need pointer arithmetic (episode 18) or stick with the indexed form.
Common mistakes
Off-by-one. for (int i = 0; i <= 5; i++) runs 6 times (0 through 5). for (int i = 0; i < 5; i++) runs 5 times. Pick the right comparison.
Modifying i inside the body. for (int i = 0; i < 5; i++) { ...; i = 10; } — the next step still happens (i++), so this might still loop. Confusing; avoid.
Inifinite loop. for (int i = 0; i < 5; ) — no step. for (;;) — no condition. Both run forever (unless the body has break).
Float counters. for (float f = 0.0; f != 1.0; f += 0.1) — never hits exactly 1.0 due to float precision. Use integer counters and divide: for (int i = 0; i <= 10; i++) { float f = i / 10.0f; ... }.
Forgetting i is local. Reading i after the loop only works if it was declared outside. Pre-C99 leakage; modern C scoping prevents it.
What's next
Episode 9: functions. Define your own subroutines. Parameters, return values, prototypes.
Recap
for (init; cond; step) { body }. Init runs once, condition checked before each iteration, step after each. Decrement: i--. Step by N: i += N. Empty parts allowed. Multiple expressions via comma operator. break exits, continue advances. C99+ lets you declare loop variables inside. For-each style doesn't exist in C — use indexed form.
Next episode: functions.