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

Overview of Program Syntax

The semicolon is used as a line terminator in several programming languages. PL/i is no exception. The rhyme and reason for deciding where to put them is as logical, or illogical, as with other languages, too. We attempt to de-mystify usage of the semicolon in this tutorial.

A PL/i program in its simplest form looks like this:

program name
declare
begin
<statement> ;
end

Note that the semicolon marks the end of the statement. (No extra space is needed between the statement and the semicolon - it is only used here for emphasis.)

We do need to put a real statement in place of <statement>. So let's try something really simple, and turn on relay #1 on AddMe III. Now our program looks like this:

program name
declare
begin
seti (25, 1);
end

This will compile and run, and turn on the relay, exactly as shown here.

Multiple statements require semicolons at the end of each statement. Declarations also need a semicolon at the end of each declaration just like statements. The simple program form grows to this:

program name
declare
<declaration 1> ;
<declaration 2> ;
<declaration n> ;
begin
<statement 1> ;
<statement 2> ;
<statement n> ;
end

The semicolon placement starts getting more interesting when we introduce a statement like if-then-else. The structure of the "if" statement is:

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

Each statement in the "if" structure has a semicolon at its end. Since the entire if-then-else structure is also a statement itself, it requires a semicolon at its end, namely following "endif". The "if" statement is broken into five lines above for clarity in structure. It is not necessary to strictly follow this form. The following structure is equally functional:

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

This "less proper" structure illustrates how it can become difficult to discern where semicolons are appropriate. They are still required to avoid compiler syntax errors. They just visually make less sense in spite of being just as essential as before.

Now that we have the basic structure covered, let's look at some variants. Here is an example of nested "if" statements:

program name
declare
a: int;
b: int;
begin
a = 1;
b = 2;
if a = 1 then
if b = 2 then
seti (25, 0);
endif;
endif;
end

Another important statement nesting structure is the begin-end statement. The begin-end is how you build compound statements that will be treated as one by statements like if-then-else. The structure of a begin statement is as follows:

begin
<statement 1> ;
<statement 2> ;
<statement n> ;
end;

Let's go back and expand our simple relay example. The following program is going to look at P/DI #1 and if it is "on", turn on relays #1 through #4. We are illustrating the use of begin-end within the "if" statement here:

program name
declare
begin
if geti(23) = 1 then
begin
seti (25, 1);
seti (26, 1);
seti (27, 1);
seti (28, 1);
end;
endif;
end

The usage of the semicolon as shown above applies in general to all statement structures including for loops, while loops, select statements, etc. Details of all of these are covered in tutorials that follow.

Comment Lines

PL/i recognizes "C++ style" comments. Anything beyond a // (double slash) on a given line will be treated as a comment and disregarded by the compiler. Compilation will resume on the next source line. If // appears at the start of a line, the entire line is discarded.