mikroC

making it simple...

mikroC - C Compiler for Microchip PIC microcontrollers

All the expressions are optional. If condition-expis left out, it is assumed to be always true. Thus, “empty” for statement is commonly used to create an endless loop in C:

for ( ; ; ) {...}

The only way to break out of this loop is by means of break statement.

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

for (s = 0, i = 0; i < n; i++) s += a[i] * b[i];

You can also do it like this:

/* valid, but ugly */

for (s = 0, i = 0; i < n; s += a[i] * b[i], i++);

but this is considered a bad programming style. Although legal, calculating the sum should not be a part of the incrementing expression, because it is not in the service of loop routine. Note that we used a null statement (;) for a loop body.

 

 

page

 

MikroElektronika: Development tools - Books - Compilers

121