| |||
| Home > RealView Debugger Keywords > Alphabetical keyword reference > for | |||
Executes one or more statements a given number of times.
for (expression_1; /* evaluate only once */expression_2; /* evaluate before each iteration */expression_3) /* evaluate after each iteration */ {statement; /* execute this statement */ /* while expression_2 is True */ [statement;]... /* additional statements */ }
The for statement is useful for executing a statement
a given number of times. It evaluates and
then evaluates expression_1 to
see if it is True, that is nonzero, or False, that is zero. If expression_2 evaluates
to True, all statements are executed once.expression_2
Next is
evaluated, and expression_3 is
evaluated again to see if it is True or False. If expression_2 is
True, all statements are executed again and the cycle continues.
If expression_2 is
False, all statements are bypassed and execution continues at the
next statement outside the for loop.expression_2
Where you have more than one statement in the for loop
these must be enclosed by curly braces ({}).
The term can
be used to initialize a variable to be used in the loop. It is evaluated once,
before the first iteration of the loop. The term expression_1 determines
whether to execute or terminate the loop and is evaluated before
each iteration. If the term expression_2 evaluates to
True, that is nonzero, the loop is executed. If expression_2 is
False, that is zero, the loop is terminated. The term expression_2 can
be used to increment a loop counter, and is evaluated after each
iteration.expression_3
This example shows how to use the for statement in a macro:
define /R void forloop()
{
int i;
for (i=0; i<11; i++) {
if (i > 10) {
$printf " Done!\n"$;
break;
} else if (i==5) {
$printf " Halfway there...\n"$;
continue;
}
$printf "Iteration: %d\n", i$;
}
}
.