Saturday, February 13, 2021

HP Prime CHOOSE for Repeated Use - Present Value

 HP Prime CHOOSE for Repeated Use - Present Value


Introduction:  Using the CHOOSE Command to Allow for Repeated Calculations


The use of REPEAT and CHOOSE will allow the programmer to set up a choose from a set of calculations and allows for additional calculations until the user exits the program.  


Here is a sample syntax, with comments followed by double slashes (//):


// Set up local variables:  a choice variable, a list of options, the size of the list

LOCAL ch, lst, s;


// Use a list of options.  Be sure to have the last option as "Exit", "Quit", etc.

lst:={"opiton 1", "option 2", ... , "Exit"};

s:=SIZE(lst);


// Set a choice variable to a starting value. Use a "dummy" value to begin with, like -1.  The choice variable will take on a positive integer value after the CHOOSE command has been executed.

ch:=-1;


// The REPEAT loop

REPEAT

CHOOSE(ch,"title",lst);


IF ch==1 THEN

[choice 1 - calculation]

END;


IF ch==2 THEN

[choice 2 - calculation]

END;


...


IF ch==n-1 THEN

[choice n-1 - calculation]

END;


UNTIL ch==s;   // This is the Exit option


Let's look at an example program.



HP Prime Program: PVACT


*  This is an example program that uses CHOOSE to allow for repeated calculations.   The application is for several types of present value for several types of annuities.


*  As a bonus, you can save some settings, such as format and number of digits, to variables before switching settings that the program requires.  This allows for the programmer to restore settings at the end of the program.  


EXPORT PVACT()

BEGIN

// EWS 2021-01-10

// CHOOSE demonstration

// loop until user exits


// Grab the settings

LOCAL h1,h2;

h1:=HFormat;

h2:=HDigits;


// ch choice varaible

LOCAL ch,n,p,r,m,w,v;

LOCAL lst,s,g;


// Set a "dummy" initial choice

// for ch

ch:=−1;


// Set a list of choices

lst:={"Constant Annuity",

"Growing Annuity",

"Perpetual Annuity",

"Exit"};

s:=SIZE(lst);


// Set decimal fix to 2 places

HFormat:=1;

HDigits:=2;


// Use a Repeat loop to allow

// more than one calculation

REPEAT


CHOOSE(ch,"Present Value",lst);

 

// Constant Annuity - Choice 1

IF ch==1 THEN

INPUT({n,r,m},"Constant Annuity",

{"n?  ","i%? ","PMT? "},

{"number of payments",

"periodic interest rate",

"payment"});

v:=Finance.TvmPV(n,r,−m,0,1);

PRINT();

PRINT("n = "+n);

PRINT("i% = "+r);

PRINT("PMT = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// Growing Annuity - choice 2

IF ch==2 THEN

INPUT({n,r,g,m},"Growing Annuity",

{"n?  ","i%? ","g%? ","PMT? "},

{"number of payments",

"periodic interest rate",

"payment growth rate",

"base payment"});

w:=(1+0.01*g)/(1+0.01*r);

v:=m/(1+0.01*r)*(1-w^n)/(1-w);

PRINT();

PRINT("n = "+n);

PRINT("i% = "+r);

PRINT("g% = "+g);

PRINT("Base Payment = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// Perpetual Annuity - choice 3

IF ch==3 THEN

INPUT({r,m},"Perpetual Annuity",

{"i%? ","PMT? "},

{"interest rate",

"payment"});

v:=m/(0.01*r);

PRINT();

PRINT("r% = "+r);

PRINT("PMT = "+m);

PRINT("PV = "+v);

// show the screen

// WAIT is needed in a loop

WAIT();

END;


// if Exit is selected

UNTIL ch==s;


// Restore the setting

HFormat:=h1;

HDigits:=h2;

END;



Eddie


All original content copyright, © 2011-2021.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Sharp EL-9300 Programs

Sharp EL-9300 Programs Today’s blog entry takes us to the 1992 Sharp’s EL-9300 graphing calculator. On January 10, 2022, I gave a revi...