User`s manual

40 digi.com Language
4.18 Program Flow
Three terms describe the flow of execution of a C program: sequencing, branching and looping. Sequenc-
ing is simply the execution of one statement after another. Looping is the repetition of a group of state-
ments. Branching is the choice of groups of statements. Program flow is altered by calling a function, that
is transferring control to the function. Control is passed back to the calling function when the called func-
tion returns.
4.18.1 Loops
A while loop tests a condition at the start of the loop. As long as expression is true (non-zero), the loop
body (some statement(s)) will execute. If expression is initially false (zero), the loop body will not execute.
The curly braces are necessary if there is more than one statement in the loop body.
A do loop tests a condition at the end of the loop. As long as expression is true (non-zero) the loop body
(some statement(s)) will execute. A do loop executes at least once before its test. Unlike other controls,
the do loop requires a semicolon at the end.
The for loop is more complex: it sets an initial condition (exp1), evaluates a terminating condition (exp2),
and provides a stepping expression (exp3) that is evaluated at the end of each iteration. Each of the three
expressions is optional.
If the end condition is initially false, a for loop body will not execute at all. A typical use of the for loop
is to count n times.
This loop initially sets i to 0, continues as long as i is less than n (stops when i equals n), and increments
i at each pass.
Another use for the for loop is the infinite loop, which is useful in control systems.
while( expression ){
some statement(s)
}
do{
some statements
}while( expression );
for
( exp1 ; exp2 ; exp3 )
{
some statement(s)
}
sum = 0;
for( i = 0; i < n; i++ ){
sum = sum + array[i];
}
for(;;){ some statement(s) }