A for loop adding up a total
A for loop runs its body once per value of the counter. Watch i go 1, 2, 3, 4 and total grow each time the body runs.
Notice the jump back up to the for line after each pass: that's where i++ happens and the condition i <= 4 is checked again. When it's false, the loop ends and i disappears, because it only exists inside the loop.
#include <stdio.h>
int main(void) {
int total = 0;
for (int i = 1; i <= 4; i++) {
total += i;
}
printf("total = %d\n", total);
return 0;
}
Output:
total = 10
From the lesson: Loops