The OPL Tutorial
By Ian Hatton
19th July 1999 - 31st August 1999
Please note that for this tutorial the programs and keywords will be in blue.
If you aren't lucky enough to own a Psion to program OPL on, the go to the Psion website and download the PC emulator of the Series 5, which is fully functional.
If you would like to write any articles about any programming languages, then please let me know.
Chapter 2 : Loops and conditionals
Coming soon:
2.3 'while .... endwh'
Chapter 3 : Dialogs
Chapter 4 : Graphics
4.1 Graphical text.
4.2 Advanced graphics.
Chapter 5 : Miscellaneous
Scrolling the screen.
Showing messages.
Calling other procedures modules.
1.1 : Printing to the screen
To print some text to the screen in OPL you use the 'print' command.
Here is an example program:
proc print:
print "Hello!"
get
endp
This program will print 'Hello!' onto the screen.
We use the command 'get' to stop the program until we press a key. If this was not there then the message would only be displayed for a very short amount of time - not long enough to read it.
1.2 : Inputting a value from the keyboard
To input a value from the keyboard in OPL you use the 'input' command.
Here is an example program:
proc input:
local a$(15)
print "What is your name?"
input a$
print "Hello", a$
get
endp
This program will print 'What is your name' onto the screen and will allow you to type an answer. It will then print 'Hello <yourname>.
To use a variable (a$) you need to declare it first with 'local' or 'global'.
When you create a variable it has to have a name. In this example it is called 'a$'.
The number in brackets after the a$ is used to specify the amount of characters the variable can hold.
To pause a program in OPL for a set amount of time you use the 'pause' command.
Luckily, this is very easy to use.
All you have to do is to specify how many 20th's of a second you want the program to pause for.
eg. To make the program pause for a second you would use the command 'pause 20'.
If you want the program to pause for a certain time, or until a key is pressed, you would put a minus (-) sign in front of the time.
eg. pause -100 to make the program pause for 5 seconds, or until a key is pressed.
To use the 'goto' command, all you have to do is to specify a label, and define when the program should jump to this label.
Here is an example program to show this:
proc goto:
local a
input a
if a=1
goto a1
else
goto anot1
endif
a1::
print "a = 1"
get
stop
anot1::
print "a <> 1"
get
end
endp
Chapter 2 : Loops and Conditionals
2.1 : The 'if .... endif' command
Using the 'if .... endif' conditional is fairly easy, as long as you stick to a few rules.
If you use the 'if' command, then you must follow it with 'endif', otherwise the program will not translate.
'if', 'elseif', 'else', and 'endif', must be used in that order.
Here is an example program to see how this works:
proc if:
local a,b
input a
input b
if a=b
print "A=B"
elseif a<b
print "A<B"
else
print "A>B"
endif
get
endp
If you try the program with different numbers, you will see it works.
In one 'if .... endif' conditional you may only use one 'if' statement, any amount of 'elseif' statements, and one 'else' statement. You may also nest an 'if .... endif' conditional inside another one.
The logical operators mean:
> greater than
>= greater than or equal to
< less than
<= less than or equal to
= equal to
<> not equal to
You now know enough of OPL to write a simple text adventure game.
2.2 : The 'do .... until' loop
The 'do .... until' loop does one, or a series of commands until a certain condition is met. Hence the name 'do .... until'.
Here is a simple example to show this:
proc dountil:
local a
a=9
do
print a
pause 10
a=a-1
until a=0
endp
Any commands between 'do' and 'until' will be executed until the end condition is met. You may also put any commands between the 'do' and 'until', although you may only nest up to 7 other 'if .... endif', 'do .... until', or 'while .... endwh' statements, otherwise you will receive a 'Too complex' error message.