Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 129

Advertising
background image

All the expressions are optional. If

condition-exp

is 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.

MikroElektronika: Development tools - Books - Compilers

121

page

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

Advertising
This manual is related to the following products: