for
Loops through a block of PSL code for a specified period.
Syntax
for ([initexpr];[termexpr];[reinitexpr]) {
{ BLOCK}
}
Parameters
Parameter | Definition |
---|---|
initexpr | PSL statement whose evaluation initializes the loop |
termexpr | PSL relational expression whose evaluation determines the |
reinitexpr | PSL statement whose evaluation reinitializes the loop |
BLOCK | one or more PSL statements that are executed once in |
Description
The for
loop behaves similarly to the C for
loop construct in that it is an iteration statement in which the following occurs:
- The first expression
initexpr
is evaluated once, initializing the loop. - The second expression
termexpr
is evaluated before each iteration and, if it becomes equal to 0, thefor
loop terminates. - The third expression
reinitexpr
is evaluated after each iteration, reinitializing the loop.
The for
loop is equivalent to the following example:
initexpr;
while (termexpr) {
BLOCK
reinitexpr;
}
Generally, initexpr
and reinitexpr
are assignments or function calls, and termexpr
is a relational expression. Any of the three expressions may be omitted; however, the semicolons must remain. A missing second expression termexpr
makes the implied test equivalent to testing a non-zero constant (or a permanently true expression).
The following example demonstrates the for
loop:
for (i = 10; i > 0; i--) {
printf(" %d seconds to go\n",i);
sleep(1);
}
The output of this example is as follows:
10 seconds to go
9 seconds to go
8 seconds to go
7 seconds to go
6 seconds to go
5 seconds to go
4 seconds to go
3 seconds to go
2 seconds to go
1 seconds to go
Comments
Log in or register to comment.