Do Statement
The
do statement executes until the condition becomes false. Syntax of do state-
ment is:
do
statement
while (
expression
);
The
statement
is executed repeatedly as long as the value of
expression
remains non-zero. The
expression
is evaluated after each iteration, so the loop
will execute
statement
at least once.
Parentheses around
expression
are mandatory.
Note that
do is the only control structure in C which explicitly ends with semi-
colon (;). Other control structures end with statement which means that they
implicitly include a semicolon or a closing brace.
Here is an example of calculating scalar product of two vectors, using the
do
statement:
s = 0; i = 0;
do {
s += a[i] * b[i];
i++;
} while (i < n);
For Statement
The
for statement implements an iterative loop. Syntax of for statement is:
for ([
init-exp
]; [
condition-exp
]; [
increment-exp
])
statement
Before the first iteration of the loop, expression
init-exp
sets the starting vari-
ables for the loop. You cannot pass declarations in
init-exp
.
Expression
condition-exp
is checked before the first entry into the block;
statement
is executed repeatedly until the value of
condition-exp
is false.
After each iteration of the loop,
increment-exp
increments a loop counter.
Consequently, i++ is functionally the same as ++i.
mikroC
- C Compiler for Microchip PIC microcontrollers
mikroC
making it simple...
12 0
MikroElektronika:
Development
tools
-
Books
-
Compilers
page