Hardware Reference
In-Depth Information
The following for loop prints out the numbers that are smaller than 100 and indivisible by 13:
for (i 5 1; i , 100; i 11 )
if (i % 13) printf(“%d “, i);
5.4.6 While Statement
During the time an expression is nonzero, the while loop repeats a statement or block of
code. The value of the expression is checked prior to each execution of the statement. The syn-
tax of a while statement is
while ( expression )
statement;
The expression is evaluated. If it is nonzero (true), statement is executed and expression is
reevaluated. This cycle continues until expression becomes zero (false), at which point execu-
tion resumes after statement. The statement may be a NULL statement. A NULL statement
does nothing and is represented by a semicolon. Consider the following program fragment:
intCnt 5 5;
while (intCnt);
The CPU will do nothing before the variable intCnt is decremented to 0. In microprocessor
applications, the decrement of intCnt is often triggered by external events such as interrupts.
5.4.7 Do-While Statement
The while and for loops test the termination condition at the beginning of a statement. By
contrast, the do-while statement tests the termination condition at the end of the statement;
the body of the statement is executed at least once. The syntax of the do-while statement is
do
statement
while ( expression );
The following do-while statement displays the integers 9 down to 1:
int digit 5 9;
do
printf(“%d”, digit 22 );
while (digit .5 1);
5.4.8 Goto Statement
Execution of a goto statement causes control to be transferred directly to the labeled state-
ment, which must be located in the same function as the goto statement. The use of the goto
statement interrupts the normal sequential flow of a program and thus makes it harder to fol-
low and decipher. For this reason, the use of goto' s is not considered good programming style,
and it is recommended that you do not use them in your program.
The syntax of the goto statement is
goto label
An example of the use of a goto statement is
if (x . 100)
goto
severe_error;
. . .
severe_error:
printf(“Variable x is out of bound!\n);
 
Search WWH ::




Custom Search