20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8.0 Statements<br />

457<br />

Execution of a break statement from with<strong>in</strong> a for, while, do, or switch statement causes<br />

execution of that statement to be immediately term<strong>in</strong>ated. Execution cont<strong>in</strong>ues with<br />

the statement that immediately follows the loop or switch.<br />

8.3 The cont<strong>in</strong>ue Statement<br />

The general format for declar<strong>in</strong>g the cont<strong>in</strong>ue statement is as follows:<br />

cont<strong>in</strong>ue;<br />

Execution of the cont<strong>in</strong>ue statement from with<strong>in</strong> a loop causes any statements that follow<br />

the cont<strong>in</strong>ue <strong>in</strong> the loop to be skipped. Execution of the loop otherwise cont<strong>in</strong>ues<br />

as normal.<br />

8.4 The do Statement<br />

The general format for declar<strong>in</strong>g the do statement is as follows:<br />

do<br />

programStatement<br />

while ( expression );<br />

programStatement is executed as long as expression evaluates as nonzero. Note that,<br />

because expression is evaluated each time after the execution of programStatement, it<br />

is guaranteed that programStatement will be executed at least once.<br />

8.5 The for Statement<br />

The general format for declar<strong>in</strong>g the for statement is as follows:<br />

for ( expression_1; expression_2; expression_3 )<br />

programStatement<br />

expression_1 is evaluated once when execution of the loop beg<strong>in</strong>s. Next,<br />

expression_2 is evaluated. If its value is nonzero, programStatement is executed and<br />

then expression_3 is evaluated. Execution of programStatement and the subsequent<br />

evaluation of expression_3 cont<strong>in</strong>ues as long as the value of expression_2 is nonzero.<br />

Note that, because expression_2 is evaluated each time before programStatement is<br />

executed, programStatement might never be executed if the value of expression_2 is 0<br />

when the loop is first entered.<br />

Variables local to the for loop can be declared <strong>in</strong> expression_1.The scope of such<br />

variables is the scope of the for loop. For example,<br />

for ( <strong>in</strong>t i = 0; i < 100; ++i)<br />

...<br />

declares the <strong>in</strong>teger variable i and sets its <strong>in</strong>itial value to 0 when the loop beg<strong>in</strong>s.The<br />

variable can be accessed by any statements <strong>in</strong>side the loop, but is not accessible after the<br />

loop is term<strong>in</strong>ated.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!