Programming HP Calculators/Commands/Branch Commands
IF
editSyntax
IF <test> THEN <clauses>: ELSE <clauses>:END:
Or for "nesting" IF statements:
IF <test> THEN <clauses>: ELSE IF <test> THEN <clauses>: ELSE <clauses>:END: ELSE <clauses>:END:
Or for running IF such that no action is performed should the test be false:
IF <test> THEN <clauses>: ELSE END:
Detail
- IF statements are a set of commands used to test certain equations or inequations. If the result is "true", then a set of actions are performed. If not, no action is performed, or a different set of actions are performed as per ELSE.
- Tests can be combined with AND or OR.
Example
PROMPT A: IF A==0 THEN MSGBOX "You entered zero.": ELSE MSGBOX "You didn't enter zero.":END:
This is a basic example, only checking for one result. Here's a more complex one, although multiple IF...THEN statements are usually best replaced with CASE.
PROMPT A: IF A==0 THEN MSGBOX "You entered zero.": ELSE IF A>0 THEN MSGBOX "You entered a positive number.": ELSE END:ELSE IF A<0 THEN MSGBOX "You entered a negative number." ELSE END:ELSE END:
Note that the additional "ELSE IF" commands displayed are not actually commands per se. What is actually happening is that the result of the previous test, when evaluated as "false", moves on to the ELSE result, where it encounters IF, telling it to start another IF statement. This is what the "ELSE END" commands do - they close off the secondary statements nested within the original statement.
CASE
editSyntax
CASE IF <test> THEN <clauses>: END ... END:
Detail
CASE is the command that is used in the place of multiple statements.
The best way of describing this is with a flowchart, seen right. Compare to the lower IF flowchart, and you can see how the CASE command is more efficient when there is more than one option.
Example
CASE is especially useful when accepting input from a CHOOSE list. For example:
1?A:CHOOSE A; "FAVOURITE APPLE?"; "Granny Smith"; "Golden Delicious"; "Pink Lady": CASE IF A==1 THEN MSGBOX "Granny Smiths are my favourite, too!": END IF A==2 THEN MSGBOX "Golden Delicious apples are OK, too.": END IF A==3 THEN MSGBOX "Bleh, I can't stand Pink Ladies!": END END: MSGBOX "Program finished.":
The first five lines initiate the CHOOSE menu. Of particular note is the 1?A at the start, this sets the default value of the menu to 1 (ie. the first option) and thus ensures that the menu operates correctly (if the value of A begins as outside the acceptable value for the menu, the highlighting will not operate correctly).
CASE initiates the CASE statement. You can then see three branches of this statement; one for each of the three menu options specified in CHOOSE. The basic syntax is:
CASE IF <clause 1> THEN <action set 1> END IF <clause 2> THEN <action set 2> END ... END:
Importantly (and counter-intuitively), the END at the end of an IF/THEN/END statement within CASE does not contain a full-colon at the end of it! However, the final END completing the CASE does require one.
You should be familiar with the MSGBOXs in each action set, these are a simple way of displaying text in a dialog and FREEZEing at the same time. The final MSGBOX is included to demonstrate what happens once the command completes; it skips to the final END:, so if you had somehow tricked the program into giving a non-valid answer (ie. changing the input for A) the program would just display the final MSGBOX. The same happens after the appropriate clause has been run for A==?.
IFERR
editRUN
editSyntax
RUN "<program-name>":
Detail
- Ceases running the current program, and runs <program-name> instead.
- You can self-reference using this command, creating an infinite loop.
Dangers
- The program name you reference is case-sensitive; that is, "ProGramNaME" is not the same as "programname".
Example
This program is called "INFINITE".
MSGBOX "You can't escape!": RUN "INFINITE":
It should be noted that you can "escape", just press ON - this usually cancels the current command from running and this feature is designed specifically to allow users to cancel programs stuck in an infinite loop due to a programming error or malicious intent by the software author.
STOP
editSyntax
STOP:
Detail
- Exits the program immediately. Compare it to BREAK.
Example
This program is called "EXAMPLE".
MSGBOX "Want to exit? 1 = yes, 0 = no.": PROMPT A: IF A==1 THEN STOP: ELSE RUN "EXAMPLE":
This command is practically self-explanatory.