Psion Siena Kingdom - Lesson3 Lesson #3

Loops and Branches

The DO...UNTIL command repeats a set of instructions until a certain predefined condition is met.

For example, I used this loop when I designed the simple program to display the characters on the Siena.

PROC Char:
LOCAL a%
a%=14
DO
PRINT a%, CHR\$(a%),
a%=a%+1
GET
UNTIL a%=255
ENDP

The instructions between the keywords DO and UNTIL are carried out until, in this case, a% is equal to 255, and then the loop finishes.

Another type of loop involves the keywords WHILE and ENDWH. In this case, the loop is completed while a certain condition is being met, and once it is not met, the loop finishes.

I have translated the above code to use the structure WHILE...ENDWH. It does exactly the same job, although slightly differently by waiting until the number exceeds 255.

PROC Char:
LOCAL a%
a%=14
WHILE a%<256
PRINT a%, CHR\$(a%),
a%=a%+1
GET
ENDWH
ENDP

Once the loop has finished, the commands below the loop are executed. For example, the once the characters have been displayed, the message "All Finished" appears on the screen.

PROC Char:
LOCAL a%
a%=14
WHILE a%<256
PRINT a%, CHR\$(a%),
a%=a%+1
GET
ENDWH
PRINT "All Finished"
ENDP

Conditional statements often use the IF...ELSEIF...ELSE...ENDIF structure.

The program that I have written below is very simple, but it demonstrates these new keywords.

PROC demo:
LOCAL a%
PRINT "Type a number between 1 & 10:",
INPUT a%

IF a%<1
PRINT "Less than 1!"
ELSEIF a%>10
PRINT "Greater than 10!"
ELSEIF a%=5
PRINT "You typed 5"
ELSE
PRINT "Thank-you"
ENDIF
PRINT "Bye"
GET
ENDP

Conditional & Logical Operators

The following operators can be used to find out how one piece if data relates to another.

 Operator Meaning < less than > greater than = equal to <= less than or equal to >= greater than or equal to <> not equal to

The logical operators that you can use are AND, OR & NOT. The logical operators can be used to check more than one condition at once.

Jumping

Sometimes it may be necessary to jump to another part in the program and this can be achieved with the use of the GOTO command. The following example shows this quite clearly.

PROC test:
PRINT "Welcome"
GOTO go
PRINT "This won't be displayed in a million years"
GO::
PRINT "This will be displayed"
GET
ENDP

In the above example, GO is a label and must have the double colon (::).

Vectoring to a label is obtained using the keywords VECTOR...ENDV. VECTOR jumps to one of a list of labels according to the value of the integer variable. The list is terminated by the ENDV statement.

The following code that I have written asks the user for a number between 1 and 3. If they choose a number outside of the range then they are reminded and the program starts again. If they do choose a number between 1 and 3, then the number they chose appears on the screen.

PROC vect:
LOCAL a%
start::
PRINT "Enter a number between 1 and 3:",
INPUT a%
VECTOR a%
x,y,z
ENDV
PRINT "Type 1,2 or 3!" : GET : GOTO start
x::
PRINT "You pressed 1" : GET : STOP
y::
PRINT "You pressed 2" : GET : STOP
Z::
PRINT "You pressed 3" : GET : STOP
ENDP

In the last example, I used a new keyword STOP and this stops the program running completely.

Other commands associated with loops

The BREAK command jumps out of a DO...UNTIL or WHILE...ENDWH structure.

The CONTINUE command jumps from the middle of a loop to its test condition. The test condition is either the UNTIL line of a DO...UNTIL loop or the WHILE line of a WHILE...ENDWH loop.

Back to Index