Reference / Paper · 1982
Pocket Computer Programming Made Easy
Jim Cole's BASIC tutorial for the TRS-80 Pocket Computer and Sharp PC-1211, with material for the PC-2 and PC-1500 (ARCsoft Publishers).
Reference / Paper · 1982
Jim Cole's BASIC tutorial for the TRS-80 Pocket Computer and Sharp PC-1211, with material for the PC-2 and PC-1500 (ARCsoft Publishers).
$8.95 POCKET COMPUTER PROGRAMMING MADE EASY latest up-to-the-minute info for using TRS-80 PC-2 and PC-1 Sharp PC-1500 and PC-1211 Casio FX-702P and other \ pocket computers AN i |. BY JIM COLE nN SSACTSSSORSS I) SSRN j \ | / ‘INCLUDES NEW TRS-80 PC-2/SHARP PC-1500 ARCsoft pocket-computer books by Jim Cole: IV vi Murder in the Mansion and Other Computer Adventures 50 Programs in BASIC for the Home, School & Office 50 MORE Programs in BASIC for the Home, School & Office 101 Pocket Computer Programming Tips & Tricks Pocket Computer Programming Made Easy 35 Practical Programs for the Casio Pocket Computer POCKET COMPUTER PROGRAMMING MADE EASY POCKET COMPUTER PROGRAMNMIMING MADE EASY BY JIM COLE ARCsoft Publishers (OODSBORO, MARYLAND 21798 FIRST EDITION FIRST PRINTING ©1982 by ARCsoft Publishers, Woodsboro, MD 21798 USA Printed in the United States of America Reproduction or publication of the contents of this book, in any manner, without express permission of the publisher, is prohibited. No liability is assumed with respect to the use of the information herein. Library of Congress cataloging-in-publication data: Cole, Jim 1940— Pocket Computer Programming Made Easy Includes index Summary: Advice for beginning computer programmers using the BASIC program language. 1. TRS-80 (Computer)—Programming. 2. BASIC (Computer program language). 3. Computers. 4. Programming (Electronic computers). |. Title QA76.8.T18C66 001.642 81-14882 ISBN 0-86668-009-8 AACR2 Trademark credits: TRS-80 is a trademark of Tandy Corporation/Radio Shack. PC-1211 is a trademark of Sharp Electronics Corp. FX-702P is a trademark of Casio Computer Company. ISBN 0-86668-009-8 Preface Today you can hold in the palm of your hand what just one generation ago required a room full of hot, chugging computer behemoths. A half-dozen electronics manufac- turers are selling hand-held battery-powered pocket-sized computers as fast as they can make them. And a dozen or more are feverishly researching, developing, designing and testing even more powerful versions of these tiny wonders. True pocket computers are here. They have computa- tion power almost beyond imagination and can com- municate in near-English. These micromachines are at the forefront of the new wave of computers for the 1990s. With such sophisticated tools at your finger- tips—literally—we won’t need the desktop dinosaurs. Stick one of those boat anchors in your attic. It’ll make a great antique for a turn-of-the-century museum. Meantime, keep your eyes peeled for ever-greater ac- complishments in handheld pocket computers. —dJim Cole Table of Contents Introduction 13 What’s Inside Your Computer 19 Writing and Running Programs 37 Input and Output 49 The Real Computer Power! 61 Superpower In Your Pocket 79 Especially Useful For Math 95 Storing Information 105 Pocket Computer BASIC Instructions 115 Error Messages 123 Introduction Introduction Computers are practical, useful, fun, even exciting. But writing programs can be a drag unless you know BASIC, the most popular program language. In this book we will introduce you to BASIC in an easy-to-understand explana- tion of the most-used words. This will be a straight-forward introduction to program- ming. We assume you have tried to read the owner’s manual which came with your computer. You know how to turn it on. You know that pushing its buttons can’t break it. Don’t be afraid to experiment. We'll show you how to make it work for you. This book is especially written for owners of the ex- citing new Radio Shack TRS-80 PC-2, the new Sharp PC-1500, the Radio Shack TRS-80 PC-1, Sharp PC-1211, Casio FX-702P Pocket Computer, Panasonic Link Hand Held, Quasar Hand Held Computer (HHC) and others. However, the knowledge of BASIC which you will gain from this book will be applicable to any microcomputer, 13 minicomputer, or main-frame computer using the BASIC language. And all of today’s popular microcomputers use BASIC. Our simple down-to-earth instruction will help you quickly understand how to talk to your computer and make it do what you want. The name of the language is BASIC. That stands for Beginner’s All-purpose Symbolic Instruction Code. What does that mean? Well, you know beginner. That’s you. All- purpose means it’s generally useful for lots of different things. Symbolic reflects the fact that the comupter uses symbols to receive instructions from you. That is, sym- bols like the word PRINT or IF or THEN or FOR or NEXT. The symbols mostly are words you already know. Code is a buzz-word used by programmers to mean instructions to a computer. So, you can translate Beginner’s All-purpose Symbolic Instruction Code to mean “You use familiar words to tell your computer how to do just about anything.” BASIC was invented at Dartmouth College in the 1960s to be used by students, beginners, novices, newcomers, to computers and programming. It’s very much like every- day English, as you’ll see as we go through this book. We'll point out the familiar look-alike words which have meanings you already know and understand. Words like end, for, go, to, if, then, list, new, next, step, print, return, run, stop, and others. Building on what you already know, we’ll show you how the computer receives your instructions and uses them to do what you want. Universal BASIC We will use what we consider the most-universal form of BASIC, simplified so it is applicable to just about any contemporary computer—large or small. These words, when used to instruct a computer, would be understood by just about any hardware. If you have a computer, other than the pocket computers mentioned here, be sure to 14 check your owners’ manual to see how its BASIC words differ (if they do) from those we use here. There are many complete programs in this book. Each has been tested on the TRS-80 Pocket Computer, the Sharp PC-1211 Pocket Computer or the Casio FX-702P Pocket Computer. Keep your owners’ manual handy as you type in and run these programs. You may need it to make sure you are properly turning on your equipment. Please remember, no two programmers write identical programs from scratch. Even when working toward the same goal, different writers will create different logic pat- terns. If your program doesn’t exactly match a suggestion in this book, yours still may be correct. Assuming your program runs and gets the required result, judgment of writing quality should be made on brevity, quickness of running time, and organizational clarity. It’s always best to write as few lines as possible. The faster a computer completes its work, the better. And instructions should appear to flow in a logical order so they can be followed by others who might read your writing. Comparison table The Appendix to this book includes a large table com- paring approximately 175 BASIC words used in the most popular pocket computers. You may find the table useful in looking up information about whether or not your par- ticular computer offers use of a particular BASIC word. The chart also will permit comparison of features if you plan to buy a new pocket computer. This is the first time such a chart has been published. We hope you find it useful. 15 What’s Inside Your Computer? What’s Inside Your Computer? Suppose you shrink to the size of an electronic mouse, hiding in a corner inside your computer, watching the ac- tion. There are four main areas which grab your attention: the input keyboard, the tiny microprocessor, a great hulk- ing memory, and the output display. Processor, input, output and memory are the important parts of any computer. There are many accessory parts and sections but those four are where the most- interesting activity occurs. Input and output, often abbreviated as I/O, allow a com- puter to receive work orders from its operator, to receive information or data for use during a work period, and to send out messages and work results to the operator. See figure 1. Input most often is a typewriter keyboard although it could be a microphone to pick up sounds, an electric eye to read printed matter, or other inventions. In today’s pocket computers, it is a keyboard. 19 OUTPUT DISPLAY t MICROPROCESSOR READ t i) WRITE | eres - — MEMORY STORAGE t WRITE INPUT KEYBOARD FIGURE 1: COMPUTER 20 Through the keyboard, an operator gives the computer a list of instructions for carrying out one or more jobs. That list, or program of action, is followed by the com- puter whenever told to do so. It does not have to be acted upon immediately. The program can be remembered for later action. To achieve some of its work goals, the computer must have additional information or data. That information also is typed in through the keyboard. So, the keyboard has two functions: sending in pro- grams of instructions and sending in additional data. The output display in pocket computers is a tiny elec- tronic typewriter and an LCD (liquid-crystal display) panel. In other computers it might be a television set or a TV-like monitor or a larger electric typewriter. The output display has one main duty: showing messages and work results to you. Memory The convenience of a computer would be lost if we had to send in instructions, one at a time, and await action after each instruction. The beauty of the beast lies in its ability to memorize a long list of instructions and then, upon later command, execute those orders. The computer has amemory to store its various lists of instructions. It is called program memory and it can hold more than one complete program at a time. At the same time, things would be slowed considerably if each extra piece of information has to be keyed in repeatedly every time the computer needed it. The com- puter can accept data one time and then store it away for repeated use later. To keep such extra information on hand, the computer has data memory. Shrunk to an electronic mouse as you are, you see that great hulking memory, stuffed with program lists and bits and pieces of miscellaneous data. Let’s take a closer look at data memory. Our microinspection tour reveals what can be imagined as a large quantity of storage boxes. See figure 2. 21 Imagine 26 boxes labeled A through Z. The contents of the boxes can be changed. Some contain something. Some contain nothing. All are variable in that their con- tents can be changed. Among the boxes containing something, you see some with numbers and some with letters and words. Consider each box to be a single memory location, identified by its label A or Bor C on through Z. Let’s look more closely at memory locations A, B and C. See figure 3. Notice that box A is empty. On the other hand, box B has a word stored in it and box C has a number stored in- side. Variable memory location Ais empty for the moment while variables B and C are holding information. Strings The boxes can contain either numerical information or words composed of combinations of letters, symbols and even numbers. Such a word is thought of as a string of data. Whenever one of our memory location boxes is stor- ing a word, it is a string variable. |f it holds only numbers, with no letters or other keyboard symbols, it is a numerical variable. The quantity of letters, symbols and numbers which can be tied together in a string and stored in one memory location is limited. In larger desktop computers, one string in one memory location can hold hundreds of characters. But in our pocket computers, one string is limited to seven characters. This limitation applies only to string variables, not to numerical variables. Here are some examples of what variables contents might look like: String Numerical Variables Variables JIM 86 @#$% ABC 1234567890 1/12/83 22.66 BIRTHDY 1 23 24 EMPTY A WORD A NUMBER FIGURE 3: A, B, & C The program writer must keep track of which kind of variable is being used in a particular memory location. For example, if you store a word in location B and then try to use that data in a math problem, an error will occur and you'll get a message from your computer. Only when you have numerical information stored ina memory location can you use that data for math. One way programmers keep such things straight is by labeling string variables with a dollar sign ($). The dollar sign means string and should be read as “string.” Empty boxes Let’s change what we saw in figure 3 to use this new idea. See figure 4. Memory location B now is correctly labeled B$, since it contains a word, while memory location C has no dollar sign since it contains numerical information. If we were to put a number in A we would label it A. If we were to put a word in A we would label it A$. A and A$ are the same storage box but you can only keep one kind of data inside at one time. By the way, you can change the contents of the various boxes during the running of a program. A location can go from empty to full or from full to empty. Or a full location can have its value changed. A program can be written so the computer will continually check memory locations to see what has been stored there. Obviously, when we say a memory location is empty we mean it has nothing in it. In effect, it has a big fat zero in- side. As a matter of fact, if you were to look at the con- tents of an empty variable, you would see that it contains a zero. If you ask the computer to show you the contents of an empty memory location, the output display will show @ if it is a numerical variable. If it is a string variable with nothing stored inside, the display will show nothing. Not even a zero. It will be blank. You write in data memory by setting the data location 25 A IS EMPTY RS HAS A WORD C HAS A NUMBER 123 26 FIGURE 4: B LABELED $ letter equal to the value you want to write in it. For in- stance: A= 1234 The value on the right is transferred into the storage loca- tion on the left. Testing Here’s a handy test of memory locations. Turn your pocket computer on. If you are using the TRS-80/Sharp computer, press the MODE button repeatedly until the display says RUN. Type in this line: A$ = “WORD” Press the ENTER key and the red CLEAR key. The main display will be blank. Note: EXE on the Casio keyboard means EXEcute and is the same as ENTER on the TRS-80/Sharp computer. Casio owners should press EXE when we call for ENTER. Similarly, Casio owners will want to press MODE and the zero key to get into the RUN mode. Pressing the MODE key and the number one will put the Casio in the program-writing mode, the same as PRO on the TRS- 80/Sharp computer. The orange AC key, which stands for All Clear, on the Casio is the same as the red CLEAR key on the TRS- 80/Sharp computer. Okay, at this point you have typed in: A$ = “WORD” and pressed the ENTER and CLEAR keys. The main display is blank. What has been accomplished? You have actually stored the letters WORD in memory location A$ inside your computer. How do you know for sure? In the RUN mode press A$ and ENTER. The string of letters forming WORD magically reappear on the display! You can CLEAR the display and recall WORD as often as you like. You have successfully stored string data in variable A and recalled it from memory for display. 27 Now try writing something else overtop of WORD in memory location A$. In the RUN mode, type in: A$ = “NAME” Press ENTER to complete the storage action and CLEAR to blank the screen. Now type in A$ and key ENTER. The new word, NAME, reappears. The old string, WORD, has been lost forever. Now, let’s put something in location B. Make it numerical data this time (not string data). Try 1234.56789 which is a nice long entry. Type this in the RUN mode: B = 1234.56789 Press the ENTER key to complete the storage and CLEAR to remove it from the display. You have stored the number 1234.56789 in memory location B. Now key in B and press ENTER. The number stored in B will reappear instantly on the display. You have successfully stored numerical data in variable B and recalled it. What’s an A$(20)? In the TRS-80/PC-1211, memory locations can be specified as A-plus-number. The number can be from 1 through 26. For instance, memory location A(01) is the first A location or, just plain old memory location A. Count down the alphabet from A. What is the thirteenth letter? It is M. Thus, A(13) is the same memory location as M. What is the twentieth letter of the alphabet? It’s T. That means A(20) is the same memory location as T. If you wish to store string data in one of these, insert the dollar sign between the A and the first parenthesis. For example, A$(02) is the same as B$ or A$(26) is the same as Z$. Your ability to specify a memory location by number like this will come in handy later. Watch for it. By the way, more powerful pocket computers such as the TRS-80 PC-2 and Sharp PC-1500 have additional memory storage boxes. These are labeled by combina- tions of two letters. Desktop computers have even more storage boxes available in data memory. 28 Flexible memories So far we have been talking mostly about data memory and ignoring program memory. We'll get to program memory in a moment. First there’s one more thing you should know about data memory locations. Among data memory locations, some are fixed and some are flexible. The fixed memory locations are those we have discussed and known as A through Z. They are always there for your use. They cannot be removed. But what if those 26 storage boxes aren’t enough? You can borrow some memory locations from program memory! Borrowed memory locations are known as flexible memory. Flexible memory in the TRS-80/Sharp computer are labeled by the A-plus-number method. Since A(01) through A(26) are the same as A to Z fixed memories, what numbers do you use for flexible memories? They start at A(27) and move up through A(28), A(29), etc., up to A(204). Storing string data in these locations makes them A$(27), A$(28), A$(29), etc. Each flexible memory used removes some storage capacity from program memory. The more flexible memories used, the shorter the programs which can be stored in program memory. You have to remember that you are using some pro- gram storage space so you don’t try to write more pro- grams and erase important data. The computer will always take back flexible memories to use as program memory when you ask it to. So, data in flexible memories could be lost. Program memory Now you know how to write information in data memory, and recall it. How about writing in program memory? To write into program memory you must put the com- puter into the program-writing mode. That’s PRO on the TRS-80/Sharp and MODE 1 on the Casio pocket computer. 29 When in that mode, anything you key in and ENTER will be stored in the program section of memory. Your computer is built to use the BASIC program language. BASIC requires each line of a program to start with a /ine number. Here’s a typical three-line program. Notice the numbers at the beginning of each line: 10 CLEAR 20 A$=‘“‘WORD” 30 PRINT A$ The computer needs those line numbers to be able to follow your instructions in sequence. It knows that line 20 comes after line 10 and line 30 comes after line 20. Here’s the same program with different line numbers: 5 CLEAR 21 A$=‘“WORD” 189 PRINT A$ This program will run just the same as the first one. The line numbers are in the same sequence and the com- mands within each line are the same. To write this program into program memory, type in one line at a time. When you are satisfied that the line is typed on the display correctly, press ENTER to complete the storage in program memory. You can’t enter more than one line at a time so press ENTER at the end of each line. Try it: 10 CLEAR Now press ENTER. Type in the next line: 20 A$=‘“WORD” Press ENTER and type in the next line: 30 PRINT A$ Press ENTER. Now you have stored the complete three- line program in memory. Now press the CLEAR key to blank the display. The pro- gram is sitting in memory but you can’t see it. To look at it, use the LIST command. Type in LIST and press ENTER. The computer will show you line 10. Use the down-pointing arrow key to go down through the line numbers. Press the arrow key once and 30 you'll see line 20. Press it again and you'll see line 30. Press the up arrow and you go up through the program lines. In the Casio computer, you don’t have the ability to scan up like this. Pressing the EXE key during a LIST will cause the next line in sequence to be displayed. LIST doesn’t work in the RUN mode on the TRS- 80/Sharp computer but it does work in the running mode on the Casio. How much is left? Anything you store in program memory takes up space and space is limited. You can tell how much space re- mains open for storage by using the MEM function on the TRS-80/Sharp computer. Type in MEM and press ENTER. With our simple three-line program typed in, the TRS-80 PC-1/Sharp PC-1211 display shows 1402 program steps or 175 memories available for programming. Suppose you wanted to get rid of the simple three-line program. In the run mode, type in NEW and press ENTER. The NEW command clears a// program memory at one time. Try it. Type in NEW. Now press ENTER. Try LIST. You should get nothing since program memory has been completely cleared. Try MEM. You now should get a message from the com- puter that it has 1424 program steps, or 178 memories, available for use. Remembering that with the program in memory there were only 1402 steps left, you can see the program used up 22 steps of program capacity. It is possible to write a program which uses every single step of program memory! CLEAR vs. NEW The command NEW erases everything stored in pro- gram memory, no matter how many different programs you have there. To erase all data memories, use the CLEAR command. It will get rid of all information in all data memories. 31 Casio note: VAC in the FX-702P is the same as CLEAR in the TRS-80/Sharp computer. CLR is the same as NEW in the program-writing mode. CLR will clear the particular program you are working in. CLR ALL will empty all pro- grams from program memory. Similarly, LIST displays only the program you are working on while LIST ALL shows all programs in program memory. The processor Be an electronic mouse inside the computer again. Notice the master-controller in charge of everything. That’s the microprocessor. Micro means small. Processor means it follows instructions in manipulating data to do work. It’s not very big but it sure is powerful! The processor is a very logical worker, dutifully going about its business in a proper order, carrying out instruc- tions, doing work. Built into the processor are instructions for how to handle its chores. As it follows that internal set of instruc- tions, it knows how to follow your external set of instruc- tions and do the work you want done. To make a long story short, the processor takes inform- ation from memory, does something with it, and then either returns data to memory or displays it as output for you to read. It is able to do this many, many times each se- cond and that’s why we love the microprocessor! Suppose you tell the microprocessor to fetch the con- tents of memory location B. It looks in there and finds WORD there. It reads that word, leaves the original behind in memory location B, and takes the information about what is in B away to work with it. The processor actually has a tiny memory inside itself so it can remember what it read in B. If we instruct the processor to store something in memory location C, it writes data to that memory location. When it writes in that memory location, it destroys whatever was there before. For example, suppose we have the number 1234 stored in memory location C. As a result of an operation, we instruct the microprocessor to 32 store the number 6789 in memory location C. It will put 6789 into C and we will lose the original number, 1234, forever. Remember: reading destroys nothing but writing replaces old information with new. In carrying out activities, the processor follows exactly the set of instructions you gave it as a program. It can’t do anything else. If you make a mistake, it makes a mistake. If your work was perfect, its work will be perfect. Program language A program is composed of sets of alphabet letters which the processor understands as words. A complete set of such words makes up a /anguage. BASIC is a language composed of words such as GO, TO, FOR, NEXT, IF, THEN, STEP, PRINT, RETURN, INPUT, PAUSE, WAIT, SET, STOP, END, SAVE, LOAD, GET, PUT, RUN, LIST, NEW and many others. Since our pocket computers are so very small, they have been given only the very best, most useful, of these words. The TRS-80/Sharp computer has all the most im- portant words. The Casio has a few more. Larger desktop computers have even more. The Radio Shack TRS-80 PC-2 Pocket Computer and Sharp PC-1500 Pocket Computer each have an extensive vocabulary, similar to the largest desktop computers. There are very few words not in their vocabularies. The more extensive the BASIC vocabulary, the more flexible the writer can be in creating programs. The total number of BASIC words invented to date is well over 500. You have the best three dozen of these in your pocket computer. It’s easy to see why BASIC is the most popular com- puter language today. It’s most like everyday English and, therefore, most readily used. 33 Writing and Running Programs Writing and Running Programs Writing programs means creating line lists of instruc- tions and storing them, one at atime, in program memory. Running means having the computer recall those sets of instructions, one line at a time, and do them. Here’s a simple one-line program. Put your computer in the program-writing mode (as explained earlier) and type in this line: 10 PRINT “WORD” When you are satisfied that you have the line correct on the display, press ENTER. That completes the act of stor- ing it in program memory. Now you can press the CLEAR key to remove the line from the display and use the LIST command to recall it. Switch to the RUN mode and type in RUN and press ENTER. What is on the display? WORD Your one line program instructed the computer to print the word, WORD, and it did just that. Now return to PRO 37 mode, type in NEW and ENTER to get rid of the one-line program, and type in this new program: 10 CLEAR 20 PRINT “WORD” Go back to the RUN mode and run it. What did you get as a result? WORD The same result. Go back to the PRO mode and type in this three line program: 10 CLEAR 20 A$=‘WORD” 30 PRINT A$ In the RUN mode, what is the result of a run? WORD Again the same result. Each of these programs looks dif- ferent but each has the same final result: printing the let- ters WORD on the computer’s display. Here’s a more-elaborate program with six lines. These lines will enhance the work result by making a sentence: 10 CLEAR 20 A$=“WORD” 30 B$= “THE” 40 C$=“IS” 50 D$=“THIS” 60 PRINT B$;A$;C$;D$ Here’s the result of running this program: THEWORDISTHIS If you had typed in a blank space at the end of each English word the sentence would have been properly spaced. For instance, if you had typed line 20 like this: 20 A$=“WORD ” the result would have looked better. THE WORD IS THIS Now you know what a BASIC-language computer program looks like. Such programs range from one-liners to lists of hundreds of lines. The pocket computer easily can store a 100-line program. 38 ENTER Whether you are trying to put program lines into pro- gram memory or general information into data memory, the computer won’t memorize typed-in info until the ENTER key is pressed. The computer’s display will echo what you keyed in, showing what you have typed, but the act of storing in memory isn’t complete until you press ENTER. Try this test. Type: WORD Press the CLEAR key. The letters WORD disappear. How can you get them back? You can’t since you didn’t tell the computer where to store them. In fact, the TRS-80/Sharp pocket computer thinks you want it to multiply W times O times R times D when you type in WORD and press ENTER. If you had stored numbers in those locations, the result of multiplication would have been displayed. Remember, you have two places to store information. Program memory for program lines with line numbers. And data memory for information labeled for one of the memory locations. To write in program memory, put the computer in the PRO mode, key in a line number, and type in WORD. Here’s an outline of the action. Your Computer Action Displays type 10 WORD 10WORD press ENTER 10: WORD press CL blank display type LIST LIST press ENTER 10: WORD What’s happened? You have entered an instruction into program memory, erased the display, and recalled the program line to the display. Now press the RUN key repeatedly to get to the RUN mode: 39 Your Computer Action Displays type WORD WORD press ENTER g What’s happended? The letters WORD have not been stored anywhere and when you tried to recall them you got a zero. Now, we know WORD is a string. Let’s turn memory location A into a string location and put WORD in it. Follow this action: Your Computer Action Displays type A$ = “WORD” A$ = “WORD” press ENTER WORD press CL key blank display type A$ AS press ENTER WORD press CL key blank display type A$ AS press ENTER WORD You have stored WORD as a string in data memory, in location A to be exact, and erased and recalled it repeatedly. RUN Let’s put an instruction in program memory and then switch to the RUN mode and run it. First, press the MODE key repeatedly to get to the PRO mode. Type in NEW and press ENTER to get rid of all old programs from program memory. Then do this: 40 Your Computer Action Displays type 10PRINT“WORD” 10PRINT“WORD” press ENTER 10: PRINT “WORD” press MODE key 3 times to RUN blank display type RUN RUN press ENTER WORD press CL blank display type RUN RUN press ENTER WORD You have successfully stored the string WORD in pro- gram memory in a command to print it when called for. Then, in the RUN mode, you called upon the computer to run the program. It did and the result was correct. The let- ters WORD were printed on the computer display. RUN is an instruction to the computer to start at the lowest program line number and begin executing com- mands it finds there. You can make the computer start its run at a different line number by typing that line number immediately after the word RUN, before ENTER. For instance, to start at line 100, type: RUN 100 and then press ENTER. The computer will skip over any program lines with numbers less than 100. Similarly, in the TRS-80/Sharp computer, you can tell the computer to start at a particular line number by put- ting a special label at the head of that line and asking the computer to RUN starting at that label. Suppose you had this three-line program in your computer. 10 CLEAR 20 PRINT “NAME” 30 “X” PRINT “WORD” To make the computer skip over lines 10 and 20 and start its run at line 30, type in RUN “X” 41 and press ENTER. The computer will search for a line star- ting with “X” such as line 30 in our program here. When it finds such a line it will start its run. REMarks Suppose you were to write a very long, 50-line program of instructions for your computer. You might forget what each line was to accomplish. You need some way to put information in program memory which won’t be acted upon by the computer during a run. Information such as notes to yourself so that when you list your program you can recall what the various parts of the program were sup- posed to do. These notes to yourself, and for other pro- grammers to read, are called remarks. The REM command is used. Anything in a program line after REM will be ig- nored by the computer during a run. For example: 10 REM PRINT “NAME” 20 PRINT “WORD” Type in this program and run it. You'll see that the com- puter has ignored, or skipped over line 10 and done line 20. Anything on a line after REM is ignored. Here’s something different. Try this one: 10 PRINT “WORD”: REM PRINT “NAME” Here the program did the first part of line 10 but ignored the last half of the line following REM. By the way, you can put multiple statements in one pro- gram line by using the colon (:). The colon indicates to the computer that a new state- ment is coming. Thus, you can place several statements in one line if you wish. Separate them with colons. Here’s an example of a one-line program including several statements: 42 10 PRINT “WORD”:PRINT “NAME”: PRINT “DOG” :PRINT “CAT” The computer will follow these statements of command in sequence as it reads through line 10. It will print WORD first, then NAME, then DOG, then CAT. REMarks are good for notes but very wasteful of memory. And we don’t have much memory to spare in the pocket computer. Use REM infrequently! BREAK What to do when your computer goes blitzo! In the PRO mode, type in this sample program: 10 PAUSE “WORD” 20 GOTO 10 PAUSE means display momentarily and then go on. GOTO means go to somewhere, in this case line 10. This small program creates a Joop. First the computer does as it is asked in line 10. It momentarily prints on the display the letters WORD. Then it goes on to line 20. At line 20 it finds a command to go back to line 10. At line 10 it momentarily prints WORD and goes on to line 20. This will cycle and recycle forever until you break the loop. Use the BREAK key. Note that BREAK is also the ON key in the TRS- 80/Sharp keyboard and the STOP key on the Casio keyboard. Let’s run our two-line loop program. Put your computer in PRO mode and type it in. ENTER each line. Then switch to RUN mode and run it. Notice that the computer is continually displaying, erasing, displaying, erasing, displaying, etc., as it loops through its instructions. Now press BREAK. The display says: BREAK AT 10 or BREAK AT 20 43 depending upon which line it was doing when you press- ed BREAK. BREAK is used whenever you need to stop a RUN dead in its tracks. It’s your panic button. STOP But suppose you want the program to STOP automatically at some point in a run? Use the STOP com- mand. Write it into your program as one line. How to continue after STOP? Use CONT. Try this sam- ple program: 10 PAUSE “WORD” 20 STOP 30 GOTO 10 Run it. The computer prints the letters WORD and then stops, displaying the message BREAK AT 20. To make the computer go on to line 30, type in CONT and press ENTER. The computer will proceed from where it left off. In this example, it will go on to line 30. At line 30 it finds an instruction to go back to line 10. At line 10 it momen- tarily prints WORD and then goes on to line 20. At line 20 it finds a STOP command so it quits and displays the message BREAK AT 20. And so on. STOP stops it. CONT makes it continue. END You can, at your option, tell the computer a program has ended. Use the END command. Try this sample pro- gram: 10 PAUSE “WORD” 20 END 30 PAUSE “NAME” 40 GOTO 10 44 Upon running this program, the computer will do line 10, printing WORD on its display. When it moves on to line 20 it will find an END statement so it will think it is at the end of the program. It ceases operations and goes to a blank display. If you were to start at line 30 with a RUN 30 command, it would first print NAME, then print WORD and then stop. The CONT function won’t work after an END command. 45 Input and Output Input and Output Input means giving the computer something to store in memory, whether data or program. Output means displaying messages and work results for you to see. INPUT Information can be permanently placed in memory when you write a program. That is, data will actually be part of the program as written. This fixed information could look like this: 10 A$=“WORD” Whenever you run the program the computer will always start with the memory that WORD is the data in memory location A$. But, suppose you want the computer to pick up changeable data during a run? Use the INPUT function. Try this program: 49 10 A$=“IT IS” 20 INPUT “WHAT IS THE WORD”,BS 30 PRINT A$;B$ When you run this program, the computer starts at line 10 and stores the string IT IS in memory location A. At line 20, the computer displays the question, WHAT IS THE WORD, and waits for a reply. You type in any string of up to seven characters in reply. Press ENTER to give your answer to the computer. The computer stores your answer in B$. Then, the computer moves on to line 30 where it recalls the contents of memory locations A$ and B$ and prints them on the display. Let’s see how INPUT works when you want to collect numerical data. It works the same. Try this short program: 10 Q=111 20 INPUT “PICK A NUMBER”,N 30 R=Q+N 40 PRINT N;“PLUS “;Q;” = “5R Here, line 10 puts the value 111 into memory location Q. Line 20 displays the message, PICK A NUMBER, and awaits your response. Whatever number you select, key it in and press ENTER. The computer will store your number in memory location N. Line 30 does the math work for you by adding. It recalls that 111 was stored in Q and your number was stored in N. It adds those two values to get a new total. The total is stored in memory location R. The program moves on to line 40. At line 40 the computer prints the results in sentence form. Try it with several different numbers. It’s fun! Suppose your number were 59. The program result, after printing line 40, would look like this: 59 PLUS 111 = 170 You don’t have to use the message part of the INPUT function if you don’t want to. For instance: 50 10 INPUT N 20 INPUT P 30 PRINT N 40 PRINT P This program allows the computer to take in your numerical data and store it in memory locations N and P and then print the values on the display. The computer will start at the lowest line number, as usual, line 10. Since no message has been supplied, the computer will display only a question mark (?). The ? tells you the com- puter wants some information. Try it on your pocket com- puter. Casio users note: use the INP key for INPUT and PRT for PRINT. Your program would look like this: 10 INPN 20 INP P 30 PRTN 40 PRT P Casio users also have available the powerful KEY func- tion which we’ll discuss later in a special section devoted to the FX-702P. PRINT You already have used the PRINT output command but here’s some further information. PRINT and PAUSE are output functions of the TRS- 80/Sharp computer. Similar Casio commands are PRT and WAIT. PRINT in the TRS-80/Sharp is the same as PRT in the Casio. PAUSE in the TRS-80/Sharp is the same as the combination of WAIT and PRT in the Casio. Here’s how they work: PRINT causes a message to be displayed on the pocket computer’s display. The printed message consists of whatever is contained within the quotation marks follow- 51 ing the PRINT command. For instance: 10 PRINT “I LIKE ICE CREAM” The computer reproduces exactly what you place bet- ween the quotes, including blank spaces. Try it in your pocket computer. Now, type in this program: 10 PRINT “I LIKE ICE CREAM” 20 PRINT “DO YOU?” Run it. Notice that the computer stops after printing the message, | LIKE ICE CREAM. Why? Because the PRINT command in effect contains a built-in STOP command. How can you make the computer show its message and then go on to the following program lines? Use PAUSE in place of PRINT. Make this change: 10 PAUSE “I LIKE ICE CREAM” 20 PRINT “DO YOU?” Now when you run the program, the computer shows the message, | LIKE ICE CREAM, for about a second and then replaces it with the line 20 message, DO YOU? If you are a Casio user, you’ll note that the TRS- 80/Sharp computer’s PAUSE combines two of your com- mands, WAIT and PRT. Your version of the same program would look like this: 10 WAIT 20 20 PRT “I LIKE ICE CREAM” 30 PRT “DO YOU?