Programming in C

(Barry) #1
8.0 Statements 457

Execution of a breakstatement from within a for,while,do, or switchstatement caus-
es execution of that statement to be immediately terminated. Execution continues with
the statement that immediately follows the loop or switch.

8.3 The continueStatement


The general format for declaring the continuestatement is as follows:
continue;
Execution of the continuestatement from within a loop causes any statements that fol-
low the continuein the loop to be skipped. Execution of the loop otherwise continues
as normal.

8.4 The doStatement


The general format for declaring the dostatement is as follows:
do
programStatement
while ( expression);
programStatementis executed as long as expressionevaluates as nonzero. Note that,
because expressionis evaluated each time afterthe execution of programStatement,it
is guaranteed that programStatementwill be executed at least once.

8.5 The forStatement


The general format for declaring the forstatement is as follows:
for ( expression_1; expression_2; expression_3)
programStatement
expression_1is evaluated once when execution of the loop begins. Next,
expression_2is evaluated. If its value is nonzero,programStatementis executed and
then expression_3is evaluated. Execution of programStatementand the subsequent
evaluation of expression_3continues as long as the value of expression_2is nonzero.
Note that, because expression_2is evaluated each time before programStatementis
executed,programStatementmight never be executed if the value of expression_2is 0
when the loop is first entered.
Va r iables local to the forloop can be declared in expression_1.The scope of such
variables is the scope of the forloop. For example,
for ( int i = 0; i < 100; ++i)
...
declares the integer variable iand sets its initial value to 0 when the loop begins.The
variable can be accessed by any statements inside the loop, but is not accessible after the
loop is terminated.

20 0672326663 AppA 6/10/04 2:01 PM Page 457

Free download pdf