# HP 48 tutorial - programming

USING LISTS

A list is a series of objects within { } delimiters. Objects within the list must be separated by a space. Many HP48 commands can be performed on a list of objects, the result also being returned as a list. This saves the need for unnecessary code, memory wastage and significantly improves program speed.

TO BUILD A LIST FROM THE STACK

Enter the elements of the list onto the stack. Enter the number of elements to be included in the list onto level one of the stack.

Press [PRG] |LIST| |->LIST|

TO ADD AN ELEMENT TO A LIST

Enter the list onto the stack. Enter the new element and press +

TO RETURN A LIST BACK TO THE STACK

Enter the list onto the stack. Press OBJ\-> The list is then split up into its respective elements onto the stack. The number of elements is also returned on level one. This number should be deleted for most uses.

LIST PROCESSING COMMANDS

Many HP48 commands can be performed on each element within a list by entering the list onto the stack and then executing the command. The resulting list is the command performed on each element of the list simultaneously.

Enter both lists onto the stack and execute the ADD command.

TO SUM ALL ELEMENTS IN A LIST

Enter the list onto the stack. ExecuteSLIST. All elements in the list are added together.

TO REVERSE THE ORDER OF ELEMENTS IN A LIST

Enter the list onto the stack, execute REVLIST. The sequence of

elements in the list is reversed.

PROGRAMMING EXAMPLES OF LIST PROCESSING:

To convert all the vectors on a stack from polar to rectangular, to find the misclose bearing and distance.

There are say five polar vectors on the stack, nothing else. The calculator is in CYLIN mode

« DEPTH ->LIST

RECTSLIST CYLIN »

Another example: To work out the proportional misclose of a similar list above.

There are say five polar vectors on the stack, nothing else. The calculator is in CYLIN mode

« RECT DEPTH 1 ->LIST DUP

ABSSLIST SWAPSLIST ABS / »

The above examples exemplify the power of list processing in survey programs and the versatility of HP48 programming.

TESTING STRUCTURES

Testing or conditional structures in a program cause 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:

< If level 2 is less than level 1 then the answer is true

> If level 2 is greater than level 1 then the answer is true

= = If level 1 and level 2 are equal then the answer is true

<= If level 2 is less than or equal to level 1 then the answer is true

= If level 2 is greater than or equal to level 1 then the answer is true

SAME If level 1 and level 2 are the same (comparing objects) then the answer is true

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.

TESTING CASES

The CASE END structure.

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 VALUES 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. (Reverse bearing)

A forward bearing is on level one of the stack

« DUP IF 180 < THEN 180 + ELSE 180 - END »

ERROR TRAPPING CONDITION TESTING

Error trapping allows for a clean exit from a program. If an error (whether forced or not) is detected during program code the program goes directly to the program code after the THEN statement.

«……. IFERR In this program THEN do this bit END »

An IFERR THEN ELSE END structure works in a similar manner to the IF THEN ELSE END structure.

PROGRAM LOOPING

Most surveying applications require repeated prompts for similar inputs (i.e. a Close program) or iteration. There are two types of loops. With DEFINITE Loops, the number of loops or iterations is known (i.e. with pre-existing data). With INDEFINITE loops there is no limit to how often a program repeats itself. Looping ceases when a certain condition is met. An indefinite looping structure is ideal for entry of data such as bearings/distances or co-ordinate data.

DEFINITE LOOPS

If the number of loops is fixed there are two general structures of performing this.

If no loop counter is required use a START NEXT/STEP Structure.

If a loop counter is required use a FOR NEXT/STEP structure.

START NEXT Structure

«…. 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 the start and finish values.

START STEP Structure

« …… 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 the start and finish values.

FOR NEXT Structure

«….. start finish FOR counter loop commands NEXT ……..»

FOR takes the numbers for the start and finish, creates a local variable (counter) and repeats the loop

commands for the integer difference between the start and finish values.

FOR STEP Structure

«….. start finish FOR counter loop commands increment STEP …….»

FOR takes the numbers for the start and finish, creates a local variable (counter) and repeats the loop

commands for the number of specified steps between the start and finish values.

INDEFINITE LOOPS

There are two types of indefinite loops:

The DO UNTIL END structure tests at the end of the loop whether to continue.

The WHILE REPEAT END structure tests at the start of the loop whether to continue.

DO UNTIL END Structure

«……… DO loop commands UNTIL test is true END ………»

The loop commands repeat themselves while the test is false.

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

WHILE REPEAT END Structure

« …….WHILE test is true REPEAT loop commands END ……. »

The loop commands repeat themselves while the test is false.

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

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 @ Clears user flag 10, Sets Degrees and polar co-ord. modes

WHILE

10 FC? @ test statement (if flag 10 clear continue program)

REPEAT

IFERR

‘Enter Data’

{’:Brg :

:Dist:'

{ 1 0 } }

INPUT OBJ->

SWAP ->V2

THEN 10 SF END @ if error then set flag 10

END

RECT 10 CF

A temporary menu can be created to improve the appearance and use of a program.

To create such a menu within a program

«…

{ label1 label2 label3 …….. }