hp-support-head-portlet

Actions
Loading...

HP Customer Support

hp-contact-secondary-navigation-portlet

Actions
Loading...

hp-share-print-widget-portlet

Actions
Loading...

hp-concentra-wrapper-portlet

Actions
Loading...

HP 20s Calculator - Branching and Conditionals

Calculator symbol key
The procedures in this document use the following text to represent symbol keys:
Key
Description
Text representation
Right shift key
RS
Left-shift key
LS
Move cursor left
cursor-left
Move cursor right
cursor-right
Move cursor up
cursor-up
Move cursor down
cursor-down
Backspace/clear
backspace
Divide
divide
Branching (GTO)
As with subroutines, it is often desirable to transfer execution to a part of the program other than the next line. This is called branching.
Unconditional branching uses the GTO (go to) instruction to branch to a program label. Use the keys: LS, GTO, then label.
The LS, GTO, label instruction transfers the execution of a running program to the program line containing that label, wherever it may be. Searching starts at LS, GTO and continues throughout all of program memory. The program continues running from the new location. It does not automatically return to its point of origin when a LS, RTN is encountered. Consequently, LS, GTO is not used for subroutines.
Conditional instructions - decisions and control
In addition to subroutines, another way to control program execution is with a conditional test - a true/false test that compares two numbers and skips the next program instruction if the comparison is false.
The HP 20s has two conditional statements: they are RS, then x<y? and RS, then x=0?. x<y? asks the questions, "Is x equal to 0? If the answer is true, the program continues execution with the line immediately following the question. If the answer is false, the program skips one line and continues from there.
For instance, if a conditional instruction is x=0?, then the program compares the contents of the display to zero. If there is a zero in the display, then the program goes on to the next line. If there is not a zero in the display, then the program skips one line and continues from there. This rule is commonly known as "Do if true."
For x<y?, the program compares y (the displayed value) with x (the hidden value). Use INPUT or any other operator (for example, + or divide) to separate x and y. If x is less than or equal to y, then program goes on to the next line. If x is not less than or equal to y (that is, x is greater than y), then the program skips one line and continues from there.
The following example illustrates conditional branching and a GTO statement.
Example of conditional branching and GTO statement
An accountant requests a program that calculates the amount of tax a person is required to pay. If the income exceeds $30,000.00, the tax rate is 38 percent. If the income is less than or equal to $30,0000.00, the tax rate is 28 percent. The question is this: is income greater than or equal to $30,000.00? Or, the question may be represented by the formula in Figure 1.
Figure : Formula for tax rate example
Keys
Display
Description
Press LS, then PRGM
Enters Program mode
Press RS, then CLPRGM
00-
Clears previous programs
Press RS, LBL, then A
01- 61 41 A
Names program
Press INPUT
02- 31
Enters display value into x-position for conditional test
Press 3
03- 3
Enters first digit of 30,000
Press 0
04- 0
Press 0
05- 0
Press 0
06- 0
Press 0
07- 0
Enters last digit of 30,000
Press RS, then x<y?
08- 61 42
Conditional test: is x less than or equal to 30,000? Does next line if true, otherwise skips one line
Press LS, GTO, then 0
09- 51 41 0
Goes to label 0 if income is less than or equal to 30,000
Press LS, then SWAP
10- 51 31
Exchanges 30,000 and income
Press x
11- 55
Multiplies x-value
Press 3
12- 3
Each digit uses one program line
Press 8
13- 8
Enters tax rate
Press LS, then %
14- 51 14
Press =
15- 74
38 percent of x-value
Press R/S
16- 26
Halts program
Press RS, LBL, then 0
17- 61 41 0
Starts routine for income less than or equal to 30,000
Press LS, then SWAP
18- 51 31
Exchanges 30,000 and x-value
Press x
19- 55
Multiplies x-value
Press 2
20- 2
Each digit uses one program line
Press 8
21- 8
Enters tax rate
Press LS, then %
22- 51 14
Press =
23- 74
28 percent of x-value
Press R/S
24- 26
Halts the program
Hold LS, and SHOW
d6b6
Checksum (see Checksum below)
Press LS, then PRGM
Exits Program mode
Test the program by comparing samples done manually. For example, pressing 15000, x, 28, LS, %, then =, should result in 4,200.0000. Test a few more incomes manually, then run the program and compare them. To run the program, enter the income value in the display and press XEQ, then A
The : that appears in the display after the program is complete is caused by the INPUT that separates income from 30,000 for the conditional test in line 08. The program can be rewritten so that x is used to separate income from 30,000 for the conditional test. Also, to save program lines, the common keystrokes can be grouped together. The following program uses conditional branching and unconditional branching for the common keystrokes.
Keys
Display
Description
Press LS, then PRGM
Enters Program mode
Press RS, then CLPRGM
00-
Clears any existing programs
Press RS, LBL, then A
01- 61 41 A
Names program A
Press x
02- 55
Puts display value into x-position for conditional. It will be used later to multiply by the tax rate
Press 3
03- 3
One digit per line
Press 0
04- 0
Press 0
05- 0
Press 0
06- 0
Press 0
07- 0
y-value is 30,000
Press RS, then x<y?
08- 61 42
If yes, goes to next line; if no, skips one line
Press LS, GTO, then 1
09- 51 41 1
Goes to label 1 if income is less than or equal to 30,000
Press 3
10- 3
Replaces 30,000 by the tax rate
Press 8
11- 8
Press LS, GTO, then 2
12- 51 41 2
Goes to label 2 for common steps
Press RS, LBL, then 1
13- 61 41 1
Starts routine 1 for x is less than or equal to 30,000
Press 2
14- 2
One digit per line
Press 8
15- 8
Press RS, LBL, then 2
16- 61 41 2
Starts routine 2 with common lines
Press LS, then %
17- 51 14
Calculates 38 percent or 28 percent ...
Press =
18- 74
... of income
Press R/S
19- 26
End of program
Hold LS, and SHOW
CbCA
Checksum (see Checksum below)
Press LS, then PRGM
Exits Program mode
Test this program the same way as the previous program for the tax rate example. Press C to remove the : from the previous example.
Checksum
After a program is entered, check to see if the keystrokes are entered correctly by comparing the checksum listed in this document to the checksum created by the program. The checksum is a unique hexadecimal value assigned to the specific keystrokes entered. To view the checksum, press and hold LS+SHOW for a moment while in Program mode.
Keystrokes for other conditionals
The HP 20s provides two of many possible conditionals using x, y, and zero. The following table shows examples of the keystrokes that can be used to create some other conditionals in a program:
Conditional
Program steps
Explanation
Press n Press RS, then x=0? Press LS, GTO, then 1 (Lines for n not equal to 0) : Press RS, LBL, then 1 (Lines for n = 0)
n is x Is n = 0? Yes. Go to LBL 1 No. Continue here
Press C Press INPUT Press n Press RS, then x<y? Press LS, GTO, then 1 (Lines for n < 0) : Press RS, LBL, then 1 (Lines for n greater than or equal to 0)
0 is x n is y Is 0 less than or equal to n? (is n greater than or equal to 0?) Yes. Go to LBL 1 No. Continue here
Press n Press INPUT Press 0 Press RS, then x<y? Press LS, GTO, then 1 (Lines for n>0) : Press RS, LBL, then 1 (Lines for n less than or equal to 0)
n is x 0 is y Is n less than or equal to 0? Yes. Go to LBL 1 No. Continue here
Press n1 Press [ - ] Press n2 Press = Press RS, then x=0? Press LS, GTO, then 1 (Lines for n1 not equal to n2) Press RS, LBL, then 1 (Lines for n1 = n2)
Is n1 - n2 = 0? (is n1 = n2?) Yes. Go to LBL 1 No. Continue here
Press n1Press INPUT Press n1Press RS, then x<y? Press LS, GTO, then 1 (Lines for n1 < n2) : Press RS, LBL, then 1 (Lines for n1greater than or equal to n1)
n2 is x n1is y Is n2 less than or equal to n1?(is n1 greater than or equal to n2?) Yes. Got to LBL 1 No. Continue here
Pressn1 Press INPUT Press 2 Press RS, then x<y? Press LS, GTO, then 1 (Lines for n1 > n2) Press RS, LBL, then 1 (Lines for n1 less than or equal to n2)
n1 is x n2 is y Is n1 less than or equal to n2? Yes. Got to LBL 1 No. Continue here
Available program memory
Program memory can have up to 99 lines. If an attempt is made to add program lines (anywhere in program memory) after 99 lines have been entered, the message "Error - FuLL" is displayed.
Non-programmable functions
The following HP 20s functions are not programmable:
  • backspace
  • LS, then cursor-down
  • LS, then cursor-up
  • LS, GTO, [ . ], line number
  • LS, GTO, [ . ], [ . ]
  • RS, then CLPRGM
  • LS and SHOW
  • LS, then LOAD
  • LS, then PRGM
  • RS, then OFF

hp-feedback-input-portlet

Actions
Loading...

hp-online-communities-portlet

Actions
Loading...

Ask the community!


Support Forum

Support Forum

Join the conversation! Find Solutions, ask questions, and share advice with other HP product owners. Visit now


hp-feedback-banner-portlet

Actions
Loading...

hp-country-locator-portlet

Actions
Loading...
Country: Flag United States

hp-detect-load-my-device-portlet

Actions
Loading...