A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C. every loop has a controlling expression. Each time the loop body is executed (an iteration of the loop), the controlling expression is evaluated; if the expression is true—has a value that's not zero—the loop continues to execute.
C provides three iteration statements: while, do, and for.
The while Statement
Of all the ways to set up loops in C. the while statement is the simplest and most fundamental. The while statement has the form
while ( expression ) statement
The expression inside the parentheses is the controlling expression; the statement after the parentheses is the loop body. Here's an example:
while (i < n)
/* controlling expression */
i = i * 2;
/* loop body */
Note that the parentheses are mandatory and that nothing goes between the right parenthesis and the loop body. (Some languages require the word do.)
When a while statement is executed, the controlling expression is evaluated first. If its value is nonzero (true), the loop body is executed and the expression is tested again. The process continues in this fashion—first testing the controlling expression, then executing the loop body—until the controlling expression eventually has the value zero.
A while statement won't terminate if the controlling expression always has a nonzero value. In fact, C programmers sometimes deliberately create an infinite loop by using a nonzero constant as the controlling expression:
while (1) ...
A while statement of this form will execute forever unless its body contains a statement that transfers control out of the loop (break, goto, return) or calls a function that causes the program to terminate.
The do Statement
The do statement is closely related to the while statement; in fact, the do statement is essentially just a while statement whose controlling expression is tested after each execution of the loop body. The do statement has the form
do statement while ( expression ) ;
As with the while statement, the body of a do statement must be one statement (possibly compound, of course) and the controlling expression must be enclosed within parentheses.
When a do statement is executed, the loop body is executed first, then the controlling expression is evaluated. If the value of the expression is nonzero, the loop body is executed again and then the expression is evaluated once more. Execution of the do statement terminates when the controlling expression has the value 0 after the loop body has been executed.
The for Statement
We now come to the last of C's loops: the for statement. Don't be discouraged by the for statement's apparent complexity; it's actually the best way to write many loops. The for statement is ideal for loops that have a "counting" variable, but it's versatile enough to be used for other kinds of loops as well.
The for statement has the form:
for ( exprl ; exprl ; expr3 ) statement
The Comma Operator
On occasion, we might like to write a for statement with two (or more) initialization expressions or one that increments several variables each time through the loop. We can do this by using a comma expression as the first or third expression in the for statement.
A comma expression has the form
exprl , expr2
where exprl and expr2 are any two expressions. A comma expression is evaluated in two steps: First, exprl is evaluated and its value discarded. Second, expr2 is evaluated: its value is the value of the entire expression. Evaluating exprl should always have a side effect: if it doesn't, then exprl serves 110 purpose.
Exiting from a Loop
We've seen how to write loops that have an exit point before the loop body (using while and for statements) or after it (using do statements). Occasionally, however, we'll need a loop with an exit point in the middle. We may even want a loop to have more than one exit point. The break statement makes it possible to write either kind of loop.
After we've examined the break statement, we'll look at a couple of related statements: continue and goto. The continue statement makes it possible to skip part of a loop iteration without jumping out of the loop. The goto statement allows a program to jump from one statement to another. Thanks to the availability of statements such as break and continue, the goto statement is rarely used.
The continue Statement
The continue statement doesn't really belong here, because it doesn't exit from a loop. It's similar to break, though, so its inclusion in this section isn't completely arbitrary, break transfers control just past the end of a loop, while continue transfers control to a point just before the end of the loop body. With break, control leaves the loop: with continue, control remains inside the loop. There's another difference between break and continue: break can be used in switch statements and loops (while, do, and for), whereas continue is limited to loops.
The following example, which reads a series of numbers and computes their sum, illustrates a simple use of continue. The loop terminates when 10 nonzero numbers have been read. Whenever the number 0 is read, the continue statement is executed, skipping the rest of the loop body (the statements sum += i ; and n++;) but remaining inside the loop.
The goto Statement
break and continue are jump statements that transfer control from one point in the program to another. Both are restricted: the target of a break is a point just beyond the end of the enclosing loop, while the target of a continue is a point just before the end of the loop. The goto statement, on the other hand, is capable of jumping to any statement in a function, provided that the statement has a label. (C99 places an additional restriction on the goto statement: it can't be used to bypass the declaration of a variable-length array.)
A label is just an identifier placed at the beginning of a statement:
identifier : statement
A statement may have more than one label. The goto statement itself has the form
goto identifier ;