Tutorial
PL/i Programming Language for i.CanDoIt
This set of pages will provide brief tutorials about programming in PL/i. This language is more simple than C, but more structured than Basic. The objective in creating this language was to compile compact code that would run efficiently and safely as a virtual machine, like a very lean Java.
Index of PL/i Tutorials
Tutorial #5

FOR (loop) Statement

The variable <var> is assigned the value of the starting expression, and after each execution of the statement, it is incremented by one until the value of <var> is greater then the value of the ending expression. If "down" is specified, then <var> is decremented by one until the value of <var> is less than the value of the ending expression.

for <var> = <expression> to <expression> <statement>;
for <var> = <expression> down to <expression> <statement>;

The <statement> may consist of multiple statements encapsulated within a begin..end; block. The <var> is any declared variable. The expressions are any valid expression producing a value that could otherwise be assigned to a variable.

begin
for i = 1 to 10
begin
level = i * 20;
seti (19, level);
delay (10);
end;
end

Note that FOR loops increment the index variable by one, and the index variable may be an int or float. If the FOR loop includes "down to" instead of "to", the index variable decrements by one. If you need an index value that increments by some number other than one, use a while loop instead as illustrated below.

WHILE DO Statement

The given statement will be executed repeatedly for as long as the expression produces a true or nonzero value. However, if the expression is false upon the first inspection, the statement will never be executed.

while <expression> do <statement>;

The "statement" may be multiple statements if they are encapsulated within a begin..end; block.

The following example illustrates the WHILE DO version of the same loop illustrated above with the FOR loop.

level = 0;
while level < 200 do
begin
level = level + 20;
seti (19, level);
delay (10);
end;
end

If you have been reading through all of the tutorials in sequential order, you have already seen the complete example from which the following snippet is taken (Tutorial #4). This illustrates a "do forever" loop, which is typically desired for any continuous control process.

begin
while TRUE do
begin
command = geti (201);
switch_relays (command);
delay (10);
end;
end