mikroC

making it simple...

Iteration Statements

mikroC - C Compiler for Microchip PIC microcontrollers

Iteration statements let you loop a set of statements. There are three forms of iteration statements in C: while, do, and for.

While Statement

Use the while keyword to conditionally iterate a statement. Syntax of while statement is:

while (expression) statement

The statement executes repeatedly until the value of expression is false. The test takes place before statement executes. Thus, if expression evaluates to false on the first pass, the loop does not execute.

Parentheses around expression are mandatory.

Here is an example of calculating scalar product of two vectors, using the while statement:

int s = 0, i = 0; while (i < n) {

s += a[i] * b[i]; i++;

}

Note that body of a loop can be a null statement. For example:

while (*q++ = *p++);

 

 

page

 

MikroElektronika: Development tools - Books - Compilers

119