Make decisions with If, Then and Else
Write a program that tells you whether a number is negative, using If, Then, Else and End.
If followed by a condition runs the next line only when the condition is true. To run several lines, put Then after the If and close the block with End. Else adds the lines to run when the condition is false.
Conditions use the TEST menu (2nd, MATH): =, ≠, >, ≥, <, ≤. Join them with and, or from the LOGIC tab.
Classic Graphing 84
Start on the main screen: Press 2nd then mode (QUIT) to get back to the home screen. Pressing clear on a menu also backs out of it.
Create a program called SIGN: PRGM, left arrow, 1, then S, I, G, N (LN, x², TAN, LOG) and ENTER.
prgm◀1lnx²tanlogenterLine 1, Prompt X: PRGM, right arrow, 2, then the X,T,θ,n key. ENTER.
prgm▶2X,T,θ,nenterLine 2, If X<0: PRGM, 1 (If), X, then 2nd, MATH, 5 for < and 0. ENTER.
prgm1X,T,θ,n2ndTESTmath50enterLine 3, Then: PRGM, 2. ENTER.
prgm2enterLine 4, Disp "NEG": PRGM, right arrow, 3, lock letters, type the quotes and N, E, G, unlock. ENTER.
prgm▶32ndA-LOCKalpha+logsintan+alphaSOLVEenterLine 5, Else: PRGM, 3. ENTER.
prgm3enterLine 6, Disp "NOT NEG" (ALPHA 0 types the space). ENTER.
prgm▶32ndA-LOCKalpha+log740logsintan+alphaSOLVEenterLine 7, End: PRGM, 7.
prgm7Quit, run it, and answer X=? with (−) 5.
2ndQUITmodeprgm1enter(−)5enter
You should seeNEG is shown, because −5 is less than 0. Press ENTER to run it again and try 3: you get NOT NEG.
Classic Graphing 84 key reference · Classic Graphing 84 manual
Tips
- Without Then, If controls exactly one line. That is handy for short checks such as If A>B:Disp A.
- Conditions are numbers underneath: true is 1 and false is 0. Type 3<5 on the home screen to see a 1.
Related guides
- Repeat steps with a For loopAdd the whole numbers from 1 to 100 with For( and End, and show the total 5050.
- Ask for numbers with Prompt and InputWrite programs that ask for values while they run and then Disp a result.
- Menus and key presses: Menu( and getKeyLet the user pick from a menu with Menu( and Lbl, or react to single key presses with a getKey loop.