Next: Omitted for-Expressions, Previous: for Statement, Up: Loop Statements [Contents][Index]
forHere is the for statement from the iterative Fibonacci
function:
int i; for (i = 1; i < n; ++i) /* Ifnis 1 or less, the loop runs zero times, */ /* sincei < nis false the first time. */ { /* Now last isfib (i)and prev isfib (i - 1). */ /* Computefib (i + 1). */ int next = prev + last; /* Shift the values down. */ prev = last; last = next; /* Now last isfib (i + 1)and prev isfib (i). But that won’t stay true for long, because we are about to increment i. */ }
In this example, start is i = 1, meaning set i to
1. continue-test is i < n, meaning keep repeating the
loop as long as i is less than n. advance is
i++, meaning increment i by 1. The body is a block
that contains a declaration and two statements.