Tuesday, November 5, 2013

HP Prime Programming Tutorial #3: WHILE, INPUT, KILL, REPEAT, GETKEY

This tutorial is going to cover a lot, each with some new programming commands in this series. I hope you are ready for the intensity. :)


WHILE, INPUT, KILL

HP Prime Program: TARGET. TARGET is a game where you provide a guess to get a desired number. If you miss, the calculator will tell you if number is higher and lower. At the end of the game, the calculator gives you how may picks you needed to get the target number.

WHILE: Repeat a number of commands while a specific condition is test.

WHILE condition is true DO
commands
END;


Access: Tmplt, 3. Loop, 5. WHILE

Caution: Watch your ENDs! Make sure an END is with each loop and the program itself. Press the soft key Check to check your work.

INPUT: Creates an input screen for variables. On the HP Prime, the input can asked for more than one input. TARGET demonstrates INPUT with one prompt.

One Variable:
INPUT(variable, "title", "label", "help text")
Multi-Variable:
INPUT(list of variables, "title", list of "labels", list of "help text")

Note: Pressing Cancel will store a 0 in variable. You may include code of what to do if the user presses Cancel, but it is not required.

Access: Cmds, 6. I/O, 5. INPUT

KILL: Terminates program execution. Nothing dies, I promise.

Access: Tmplt. 1. Block, 3. KILL


Program:
EXPORT TARGET()
BEGIN
LOCAL C:=0, N:=RANDINT(1,20), G:=-1;
WHILE G≠N DO
C:=C+1;
INPUT(G,"Guess?","GUESS:","1 - 20");

IF G==0 THEN
KILL;
END;

IF G < N THEN
MSGBOX("Higher");
END;

IF G > N THEN
MSGBOX("Lower");
END;

END;

MSGBOX("Correct! Score: "+C);

END;


Try it and of course, you can adjust the higher limit. Here is some thing for you to try with TARGET:

1. Add a limited amount of guesses.
2. Can you display the list of guesses?


REPEAT

ULAM Algorithm: take an integer n. If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. ULAM counts how many steps it takes to get n to 1.

REPEAT:

Access: Tmplt, 3. Loop, 6. REPEAT

Featured:
CONCAT(list1, list2): Melds list1 and list2 into one.

Access: Toolbox, Math, 6. List, 4. Concatenate


EXPORT ULAM(N)
BEGIN
LOCAL C:=1, L0:={N};

REPEAT

IF FP(N/2)==0 THEN
N:=N/2;
ELSE
N:=3*N+1;
END;

C:=C+1;
L0:=CONCAT(L0,{N});
UNTIL N==1;

MSGBOX("NO. OF STEPS="+C);
RETURN L0;
END;


Examples:

ULAM(5) returns:
Message Box: "NO. OF STEPS=6"
List: {5, 16, 8, 4, 2, 1}

ULAM(22) returns:
Message Box: "NO. OF STEPS=16"
List: {22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1}



GETKEY

The next section will introduce a super-important command, GETKEY. We will be working with GETKEY over the entire series.

The Program KEYNO: The person presses key presses. Which each key press, the code returns to the terminal screen. The program terminates when the Enter key is pressed.

GETKEY: Returns the key code of last key pressed. The Prime's key map is below. (Picture is from the HP Prime User's Guide)

Access: Cmds, 6. I/O, 4. GETKEY



EXPORT KEYNO()
BEGIN
LOCAL K;
PRINT();
PRINT("Press any key to get its code.");
PRINT("Press Enter to exit.");

REPEAT
K:=GETKEY;
IF K ≥ 0 THEN
PRINT(K);
END;
UNTIL K==30;

END;


Example Key Codes:
33: 8 key
2: up
7: left
8: right
12: down
50: plus
45: minus


This concludes Part 3. Again, it can't be said enough, thanks for all the comments and compliments. And until next time,

Eddie


This blog is property of Edward Shore. 2013

6 comments:

  1. Hi!
    Thanks for the tips!
    I have been trying to find information about text variables in the HP Prime.

    I mean, how can I define text (string) variables?
    For example:

    EXPORT ROD()
    BEGIN
    LOCAL a1:=0;
    INPUT(a1,"enter a number","ENTER:","Enter a number please");
    IF a1 == 0 THEN
    KILL;
    IF a1 > 0 THEN
    [TextVariable] := "Hi!"
    IF a1 < 0 THEN
    [TextVariable] := "Bye!"
    MSgBOX([TextVariable]);
    END;

    Is there a way to do this?

    Thanks in advance!!!

    ReplyDelete
    Replies
    1. Darn I feel like a ass, I just had to "declare" the text variable in the LOCAL section...

      EXPORT ROD3()
      BEGIN
      LOCAL a1:=0, TextVariable:="";
      INPUT(a1,"enter a number","ENTER:","Enter a number please");
      IF a1 == 0 THEN
      KILL;
      END;
      IF a1 > 0 THEN
      TextVariable := "Hi!";
      END;
      IF a1 < 0 THEN
      TextVariable := "Bye!";
      END;
      MSgBOX(TextVariable);
      END;

      that is what I did and it worked.

      Thank you!!!!!

      Delete
    2. Great that it worked out! Thank you!

      Delete
  2. HI
    ON HP 50G I use (do -- until -- end)loop command for check some command at least
    1 time. how can i do it on prime.

    on hp 50g :

    DO
    IF %dt TYPE 5. \=/
    THEN "\029 Beem Analyze" { { "As" 1. } { "As + As`" 2. } { "\Gr" 3. } { "\Gr + \Gr`" 4. } }
    ELSE "T Beem Analyze" { { "As" 1. } { "\Gr" 3. } }
    END 1. CLLCD CHOOSE
    IF 0. ==
    THEN 1. '\Gr' STO 1. 'ch' STO
    ELSE \-> ch1
    \<<
    CASE ch1 1. ==
    THEN " Continue with " { { "As balance" 4. } { "Add As " 1. } { "As minimum" 2. } { "As maximum" 5. } { "Unknown As" 3. } } 1. CLLCD CHOOSE DUP 'ch' STO
    ..........

    END
    \>>
    END
    UNTIL ch 1. ==
    END

    ReplyDelete
    Replies
    1. Arash,

      Consider a REPEAT-UNTIL clause for the HP Prime:

      REPEAT
      IF type(var) = required type
      THEN
      CHOOSE(\029, "Beem Analyze", {"As","As + As`","\Gr","\Gr + \Gr'"};
      etc.....


      UNTIL ch==1;


      Hope this helps,

      Eddie

      Delete
  3. Great tutorial as usual Eddie!!!!!

    ReplyDelete

HP Prime and TI-84 Plus: Basic Wheatstone Full Bridge Circuit

HP Prime and TI-84 Plus:  Basic Wheatstone Full Bridge Circuit The program WHEATSTONE (HP Prime) and WHTSTONE (TI-84 Plus CE) deals wi...