TUTORIAL 5: Branching Structures
Testing Structures

Testing or conditional structures in a program causes the flow of operations of a program to follow a different branch or path based upon a test.

IF test is true THEN do this bit END

The program performs a test after the IF statement. If the test is true the program goes to the program commands after the THEN statement. If the test is false the program goes to straight to the END.

Test commands are as follows: < > == SAME <= >= and so on. An example of program code: if B is greater then A (true) then make B negative:

<< .... IF B A > THEN B NEG 'B' STO END .... >>

IF test is true THEN do this bit ELSE do this bit instead END

The program performs a test after the IF statement. If the test is true the program goes to the program commands after the THEN statement. If the test is false the program goes to the program commands after the ELSE statement.

An example of program code: if B is greater then A (true) then make B negative, else (if B is smaller than A) make B = 0:
<< .... IF B A > THEN B NEG 'B' STO ELSE 0 'B' STO END .... >>

Test commands can be 'nested' i.e. an IF THEN END structure can be included in the test commands.

CASE END

If several tests are to be formed this is a good structure to use

<< CASE
test 1 THEN do this END
test 2 THEN do this END
.
.
test n THEN do this END
END

The program performs each test in sequence until the test is 'true'. When the test is true, the program code after the THEN statement is executed. The code then goes straight to the end of the whole CASE END structure.

Testing a value on the stack

An example of program code. The code tests the value of level one of the stack. If the value is less than 180 then add 180 to it. If the value is greater than 180 then subtract 180 from it.

A value is on level one of the stack
<< DUP IF 180 < THEN 180 + ELSE 180 - END >>

Definite loops

If a series of commands is to be repeated a 'known' or definite number of times use:

<< start finish START loop commands NEXT >>
START takes the numbers for the start and finish and repeats the loop commands for the integer difference between them.

<< start finish START loop commands step STEP >>
START takes the numbers for the start and finish and repeats the loop commands for the number of the specified steps between them.

FOR NEXT also performs a similar process (see p.29-13 User Manual)

In-definite loops
If the number of loops is unknown (i.e. iteration) these commands facilitate this.

<< DO loop commands UNTIL test is true END >>

The test command is what determines whether to exit the program.

<< WHILE test is trueREPEATloop commandsEND>>

Error trapping
Error trapping allows for a clean exit from a program.

<< IFERR In this program THEN do this bit END >>

IFERR THEN ELSE END Also works

An example of a more complex program
A program allowing indefinite data entry for a close, building a stack of polar vectors representing legs.

<< 10 CF DEG CYLIN
WHILE 10 FC?
REPEAT
IFERR
"Enter Data"
{":Brg :
:Dist:"
{ 1 0 } }
INPUT OBJ->
SWAP ->V2
THEN 10 SF END
END
RECT 10 CF
>>

Go to the last tutorial

Return to
Tutorial page| Programming tips| Quickclose Homepage|

QUICKCLOSE
ABN 68 801 812 834
PO Box 1035 LEICHHARDT NSW 2040 AUSTRALIA
Tel/Fax +61 +2 9550 0010
EMAIL : quickclose@quickclose.com.au

Created with EditPad
Last Update: 18th March 2001
Richard Stanaway