C Tutorial
C For Loop
for packs init, condition, and step in one line. Use it when you know how many times to repeat.
Three parts in one line
A for loop has three slots separated by semicolons: initialize, condition, step. The initialize runs once. Then C checks the condition, runs the body, runs the step, and checks again, until the condition is false.
That is the same three jobs a while loop has, written where you can see them together. Usefor when the number of passes is known: 0 to n minus one, 1 to 10, every index in an array.
A complete for program
Declare the index in the init slot: int i = 0. That C99 form is the style this tutorial uses.i < 5 is the test. i = i + 1 is the step. The values printed are 0, 1, 2, 3, 4 — five times, not six.
Example
#include <stdio.h>
int main(void) {
for (int i = 0; i < 5; i = i + 1) {
printf("%d\n", i);
}
return 0;
}Run this in /c/try. Counting from 0 is the usual C habit because array indexes start at 0.
Read the header left to right
| Slot | This example | When it runs |
|---|---|---|
| Init | int i = 0 | Once, before the first check |
| Condition | i < 5 | Before every pass, including the first |
| Step | i = i + 1 | After each pass, before the next check |
i++ means the same as i = i + 1 here. You will see ++ in other people's code. Either form is fine for a counter.
The variable declared in init exists only inside the loop. After the closing brace, i is gone.
Count the other way
Start high and subtract if you need a countdown. The condition still has to become false. Herei goes 5, 4, 3, 2, 1, then i >= 1 fails.
Example
#include <stdio.h>
int main(void) {
for (int i = 5; i >= 1; i = i - 1) {
printf("%d\n", i);
}
printf("done\n");
return 0;
}Walk a list with an index
C has no range-for. To visit every slot of a fixed array, write a classic for withint i = 0 and stop at i < n. Arrays are the next chapters; this is enough to print a short list.
Example
#include <stdio.h>
int main(void) {
int nums[3] = {2, 4, 6};
for (int i = 0; i < 3; i = i + 1) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}Off-by-one
i < n runs n times when i starts at 0. i <= n runs n + 1 times. Both are valid; they mean different things. For an array of length n, the last valid index is n minus one, so the test is i < n.
Next: break and continue — leaving a loop early, or skipping one pass.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Physics
A drop, second by second
Ticker-tape and strobe photos show position at equal time steps. From rest, s = ½ g t². At t = 1, 2, 3, 4 s the distances are about 4.9, 19.6, 44.1, 78.5 m. The gaps grow because the object is speeding up.
A for loop is the natural fit when you know the times in advance. Change g and the table is a different planet.
s(t) = ½ g t²
Example
#include <stdio.h>
int main(void) {
const double g = 9.81;
int t;
for (t = 1; t <= 4; t++) {
double s = 0.5 * g * t * t;
printf("t = %d s, s = %.2f m\n", t, s);
}
return 0;
}Maths
Sum of squares
1² + 2² + … + 10² is 385. The closed form n(n+1)(2n+1)/6 gives the same 385 for n = 10. The loop is how you check the formula before you trust it on n = 1000.
Adding n * n each time is the definition. Off-by-one errors show up here: i <= 10 versus i < 10 is a 100 missing at the end.
Σ k² = n(n+1)(2n+1)/6
Example
#include <stdio.h>
int main(void) {
int n;
int sum = 0;
for (n = 1; n <= 10; n++) {
sum += n * n;
}
printf("sum of squares = %d\n", sum);
return 0;
}