Programming in Tiny Basic

2 -- Running Programs

<Prev  Next>


Every time you have typed something into your computer, It responded immediately to do what you told it to do. That is gratifying, but it sometimes is not very useful. In this chapter you will learn how to tell your ELF to remember several things to print, and how later to tell it to print those things.

It is actually quite easy. In front of the command to PRINT something, you type a number (say like 10). When TINY sees the number, it saves the line in the computer's memory. Then you can type another line with a different line number, and TINY will save that one also. First clear the computer memory by typing CLEAR (and a carriage RETURN, of course). Then type in:

10 PRINT "2+3=0;2+3
20 PRINT (3*24-12)/2;" DAYS HATH SEPTEMBER"
Notice that the only thing TINY BASIC prints out after you hit the carriage return on each of these lines is a new line prompt. We need one more thing before we can get this to print out. We need to tell TINY that this is all we want it to do. You do this by typing
99 END
This line is stored in the computer's memory as the last line. You do not need to type it again once it is in the memory in the right place.

Now I can almost hear you ask, "How do I know these lines were saved in the computer's memory?" Well, we can ask the computer for a list of all the lines in its memory, and see what it says. Type

LIST
You will see, on your screen, something that looks a little bit like this:
10 PRINT "2+3=0;2+
.3
20 PRINT (3*24-12)
/2;" DAYS HATH SEP
TEMBER"
99 END
:
Of course you have to realize that there are really only three lines in memory, and that they have been broken up into six pieces for display on your screen. You know these lines are in memory, because you can PRINT out anything else you wish (without line numbers) or hit the RETURN key seven times and then LIST again, and you will get the same display. What you have done is entered a program into your computer's memory.

Now suppose you made a typing error in entering your program into memory. After you hit the carriage return key on the keyboard, there is no amount of ESCaping or backspacing that will correct that error. The line has been stored, error and all, into memory. But all is not lost! Just type the line in again with the same line number, the way you really intended it to be, and the new version will replace the old line in memory. If your mistake is in the line number itself, you will have to type the line with the correct line number, then on a separate line, just the line number of the line that is wrong. If you were lucky and made no mistakes in our little example, you might try changing line 10 to print the sum of 3+14 instead of 2+3. After you do this, LIST the program again to be sure you typed what you wanted.

You can run this program just as easily. Just type RUN with a carriage RETURN, and there, underneath the line you just typed (i.e. under the word, "RUN") will be the results of the two commands you typed In earlier. Type RUN again. And again. You see, as long as the program is in the computer's memory, you can RUN it over and over again. This is one of the useful things about computers. Though it may take a long time to get a program into the computer correctly, after it is, you can run it many times and get exactly the same results.

But suppose those results are not exactly what we wanted. Suppose, for example, that we want a blank line on the display between the two output lines. A blank line, as you recall, happens if you type in the command PRINT with nothing else on the line. If we put a line number on a line that says only PRINT, then when the program is RUN, that command will print a blank line. But how do we get it between the other two commands In the program? You could just retype the whole program with the new line in its proper place, but that is much too much work. Instead we will choose a line number between the other two line numbers -- say 15:

15 PRINT
Type it in, then LIST the program again. Aha! It seems to be in the right place, but the first line ran off the screen before you got a good look. Type LIST again, but this time, after the screen has blinked two or three times, reach over and push the "|" key on the corner of your ELF II. As long as you hold that key down, the display stops moving so you can look at it. When you release the key, the LISTing resumes, but you can always hold it again by pressing the "|" key again.

Anyway, now you can clearly see that line number 15 is between lines 10 and 20. It does not matter at all that this was the last line you typed. In fact you can type lines in any order you please, and TINY BASIC will put them in the correct order. This is why we usually do not start our numbering with line 1 or go up by sequential numbers. Often we will think of something later to add to the program, and if there are unused numbers at the point we want to make the change, we can just type it in. If the line numbers are sequential then we have to retype the program with the new lines inserted. I am much too lazy for that.

By the way, don't forget to RUN this new version of the program to see what the effect of line 15 is.

Now let's add another line to the poem. What line number should we use? That's right, 30 is a good number. Make it print "APRIL, JUNE". List the program to be sure it is right, then RUN it. Add another line to print "AND NOVEMBER." Test it (ie. Run it, and be sure it looks right). Gee, that first line is getting lost off the top of the display when the last version is run. Why don't you just remove that command from the program. Removing a line is easy: you just type the line number, followed by a carriage return, TINY replaces that line with aline that has nothing on it, and the line vanishes from the program. Try it, then LIST the program.

Now, while you are at It, why not pretty up the first line of the poem? Splitting a word in the middle of a letter is not particularly beautiful, so you should figure out how to divide the phrase into two different lines. Notice that long lines split differently when you LIST them than when you RUN them: we are interested in what it looks like when you RUN. One of the new lines you can number 20 again; the other you might number 25. You will have to type in both lines, because TINY does not know how to make two lines out of one. Do that now, and test the resulting program. Are you happy with it? Why don't you show off your skill with the computer to some appreciative friend. You could clear the screen, type ESCape, then RUN (without the carriage RETURN) when you are ready to demonstrate your program, just type the RETURN key.

You should probably practice thinking up things for your computer to print out on the screen, then writing the programs to print them. Each time it is helpful to remove the old program before putting In the now. Retyping all those line numbers is a bit of a chore, but the whole program can be removed at once by typing the command CLEAR. Do not confuse clearing memory (by the CLEAR command) with clearing the TV screen (by typing control-L).

When you put the next program in, don't forget that the last line must be an END command. If you leave it out, you will get an error message something like

!75 AT #0
If (maybe I should say When) you make other mistakes in typing your program, TINY will give other error messages, mostly like the ones you have already seen, except that if they are noticed after you type RUN, they will come with a line number to help you find which line has the error. Suppose you program has the line in it somewhere,
47 PRINT "HELLO. I AM
When you try to run this program and it comes to this line, it will print out
HELLO. I AM

!61 AT #47

Error number 61 tells you that you left off the close quotation mark. the "AT #47" tells you that the error occurred in line number 47. To look at just that line you can request of TINY:
LIST 47
or you can request that TINY list several lines by typing two line numbers on the LIST command:
LIST 30,50
lists all the lines in the program betwen 30 and 50, inclusive.

Now you can write, modify, and run programs in TINY BASIC. But there is more! In the next chapter you will learn how to store numbers in the computer.
 

Back to Top
Continue with: 3 -- Variables: Changing the Computer's Mind