Page 178 - The Ultimate Palm Robot
P. 178

Color profile: Generic CMYK printer profile
           Composite  Default screen
                                                          Bots /The Ultimate Palm Robot/ Mukhar & Johnson / 222880-6 / Chapter 7






                                                  Chapter 7  Essential Robot Programming Strategies  161



                                    asks the user for his name. When the program jumps back to the start of the
                                    loop, the program prints “Hello,” with the name that was entered.

                                   x$ = input$(1) : rem wait for button press
                                   i$ = inputbox("What is your name", "Name Entry", 2)

                                      The inputbox( ) function can take three arguments. The first is a query
                                    string displayed in the box, and the second is a form title. The final argument,
                                    2, tells the function to display both OK and Cancel buttons. Finally, the pro-
                                    gram checks that the user has actually entered something. If the entered string
                                    is not empty, it is saved in variable b$. When the program loops back to the
                                    start, it will draw the phrase using the new value of b$.


                                   if i$ <> "" then b$ = i$ : rem if user entered something, store it in b$
                                   wend : rem end of loop, go back to start

                                      Here is the complete program listing:


                                   #HelloWorld.bas
                                   a$ = "Hello,":b$= "World!"
                                   while (1) : rem loop forever
                                   draw –1 : rem clear the screen
                                   draw a$ + b$,2,15
                                   form btn 2,30, 55,15, "Enter Name", 1
                                   x$ = input$(1) : rem wait for button press
                                   i$ = inputbox("What is your name", "Name Entry", 2)
                                   if i$ <> "" then b$ = i$ : rem if user entered something, store it in b$
                                   wend : rem end of loop, go back to start
                                   end

                                      There are a few more things we want to point out about the code above.
                                    First, while BASIC programs are usually line-numbered, the code above is
                                    not. HotPaw Basic will automatically apply line numbers internally. Line
                                    numbers can be useful when you are debugging the code and when you want
                                    to create subroutines. We’ll do that in a later program. Also, comments in the
                                    code start with the keyword rem. Anything that follows rem up to the end of
                                    the line is a comment and is not executed. Finally, the program ends with the
                                    keyword end. The end keyword is not really necessary in this program be-
                                    cause it runs in an endless loop. However, it is useful to get in the habit of putt-
                                    ing an end at the end of your main program. Later in this chapter, we will write
                                    a HotPaw Basic program that uses subroutines that follow the main program.









           P:\010Comp\Bots\880-6\ch07.vp
           Monday, May 12, 2003 4:29:22 PM
   173   174   175   176   177   178   179   180   181   182   183