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 #4

IF THEN ELSE Statement

The "if-then-else" statement has the following general syntax:

if <condition> then <statement> else <statement> endif;

The "else" part of the command is optional, thus the "if" statement can have the format

if <condition> then <statement> endif;

The <condition> is any valid comparison or expression. The <statement> is any single statement terminated by a semi-colon. A compound statement (series of statements) may be encapsulated in a begin..end to cause them to statements to be treated as a single statement. The entire statement may be spread over several lines of source code, for example:

if myVar1 > myVar2 then
begin
myOtherVar = geti (100);
myLastVar = myVar1 * myOtherVar;
seti (100, myLastVar);
end;
else seti (100, 0);
endif;

Conditional operators:

=
Equal
<>
Not equal
>
Greater than
>=
Greater than or equal
<
Less than
<=
Less than or equal

Logic operators for conditional expressions:

and
Logical AND, e.g., if (a > b) and (c > d) then ...
not
Logical NOT, e.g., if not (a > b) then ...
which is of course the same as if a <= b then ...
or
Logical OR, e.g., if (a > b) or (c > d) then ...

Be sure to use parenthesis to establish precedence as necessary. If you start with this statement:
if (a > b) or (c > d) then
but remove all of the parenthesis, like this:
if a > b or c > d then
it will get interpreted as if you had put parenthesis back like this:
if ((a > b) or c) > d then
This will not yield the same result as the first statement.

SELECT Statement

The select statement is analogous to the C case statement, or the Basic ON GOTO statement.

select
when <expression> <statement>;
when <expression> <statement>;
...
when <expression> <statement>;
otherwise <statement>;
end;

The statement is executed when its corresponding condition tests true or nonzero. Multiple expressions may test true in a given select statement. If none of the expressions tested true by the time "otherwise" is reached, the otherwise statement is executed. It is not illegal for additional "when" clauses and another "otherwise" to follow the otherwise. Each subsequent otherwise is a cumulative test of any conditions testing true so far. Each instance of "statement" may be multiple statements if encapsulated in a begin..end; block. The following example illustrates all of these.

program test
declare
command: int;

procedure switch_relays (relay: int)
declare
begin
select
when relay = 1 seti (25, 1);
when relay = 2 seti (26, 1);
when relay = 3 seti (27, 1);
otherwise
begin
seti (25, 0);
seti (26, 0);
seti (27, 0);
end;
when relay = 4 seti (28, 1);
otherwise seti (28, 0);
end;
end;

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

The above procedure takes a command code and switches relays based on that code. If the code is 1, relay #1 (register 25 on AddMe III) is turned on. If the code is 2, relay #2 is turned on. If the code is 3, relay #3 is turned on. If the code is 4, relays #1 through #3 are turned off and relay #4 is turned on. If the command code is anything other than 1 through 4, all relays are turned off.

The program will take the command from register 201 and act on it once a second, forever.