for

Loops through a block of PSL code for a specified period.

Syntax

for ([initexpr];[termexpr];[reinitexpr]) {
{ BLOCK} 
}

Parameters

ParameterDefinition
initexprPSL statement whose evaluation initializes the loop
termexpr

PSL relational expression whose evaluation determines the
continuation or termination of the loop

reinitexprPSL statement whose evaluation reinitializes the loop
BLOCK

one or more PSL statements that are executed once in
accordance with the evaluation of the expressions


Description

The for loop behaves similarly to the C for loop construct in that it is an iteration statement in which the following occurs:

  1. The first expression initexpr is evaluated once, initializing the loop.
  2. The second expression termexpr is evaluated before each iteration and, if it becomes equal to 0, the for loop terminates.
  3. 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

Where to go from here

PSL Statements

Was this page helpful? Yes No Submitting... Thank you

Comments