BASIC Programming For Kids (1983)
BASIC Programming For Kids (1983)
BASIC
Programming
for KIDS
BASIC Programming on Personal Computers by
APPLE
ATARI
COMMODORE
RADIO SHACK handsome lake library
TEXAS INSTRUMENTS greenburgh-h ufsd
TIMEX SINCLAIR D0BBS FERRY'NY10522
Apple and the Apple logo are registered trademarks of Apple Computer,
Incorporated.
ATARI and the Design arc registered trademarks of Atari, Incorporated.
400/800 and 1200XL are trademarks of Atari, Incorporated.
Atari, Incorporated, was not in any way involved in the writing or other
preparation of this book, nor were the facts presented here reviewed for ac
curacy by that company. Use of the term ATARI should not be construed
to represent any endorsement, official or otherwise, by ATARI, Incor
porated.
Commodore and the Commodore logo are registered trademarks of Com
modore Business Machines, Incorporated.
TRS-80 is a trademark of the Radio Shack Division of Tandy Corporation.
Texas Instruments and the TI Monogram are registered trademarks of
Texas Instruments Incorporated.
q 10 9 8 7 6 5
Acknowledgments
This book really owes its conception to C. Michael and Michael C. Ault —
the first for providing the wherewithal and the second for discovering the why
and wherefore. In other words, it was my husband who had the idea to buy a
computer in the first place and my son who was able to figure out what to do
with it! This book couldn't have been written without their insights and collab
oration.
Special recognition also should go to Ben Ault for his program ideas, and to
the Hardy School parents, teachers, and children for the support, encourage
ment, and enthusiasm that made this project possible. Former teacher Nancy
Gouveia deserves special mention for pointing the way to an elementary com
puter program in our schools. Other valuable input came from Tom Vaughn
and his teaching teamwork. Thanks, too, to Frank Smith and Sheila and John
Gill for the generous loans of their computers.
Finally, I want to acknowledge the influence of Russ Walter in this book's
approach to computing. For me, Russ's publications (The Secret Guide to Com
puters, 92 St. Botolph Street, Boston, MA 02116) were the first to cut through
the jargon and abstruseness that usually obscure this subject, proving that
computers can be easy and fun for just about anybody.
Contents
Introduction V
1. All Keyed Up 1
2. 1,2, 3 —Run! • 30
3. Drive the Computer Loopy 41
4. Your Number, Please? 47
5. All Tied Up in Strings 60
6. Just Fooling Around 67
7. Yes and No with IF and THEN 79
8. Clean Up Your Act 90
9. Make Your Computer a Gambler 101
10. You Can Count on Your Computer 114
11. DATA: It's What Your Computer Loves to READ 131
12. Putting It All Together 145
Appendix I: Quick Reference Guide 162
Appendix II: Troubleshooting 165
Appendix III: Editing Your Mistakes 168
Appendix IV: What Is a Computer? 175
Index 183
Introduction
This is a book for children, parents, teachers — and anyone else who likes to
learn. Its purpose is to help kids (and adults) to begin exploring an important
and fascinating subject — computers.
A great deal has been said and written about the "computer revolution." It is
here, and it is affecting all our lives in ways we don't even know about yet. The
younger you are, the more impact computers will have on you — since you
presumably have more years ahead to live in a world with more and more
computers in it.
In order to use this book you should have (or have access to) a computer that
"speaks" the computer language BASIC. (Every popular brand of general-pur
pose microcomputer now being sold for homes and schools uses BASIC.) This
book will teach you how to write simple programs in BASIC for your com
puter. Its purpose, however, is not to make you a programmer. Its purpose is to
help you understand computers, to think about how computers can help you in
all kinds of ways, and to discover how much fun you can have when you learn
how to talk to computers.
Introduction
VI
1.
All
Keyed
Up
In this chapter you will be learning about the keyboard on your computer. At
the time this book is being written, the most common computers for homes and
schools are:
* Apple 11+ or He Tl Texas Instruments 99/4A
A. ATARI 400 or 800 T/S Timex/Sinclair 1000
C< Commodore ( VIC 20, 64, or PET) TRS TRS-80 {Model /, 777, and Color)
If you have one of these computers, you'll find specific instructions for your
machine in this book. But you should be able to use this book with just about
any personal computer. The examples will work pretty much the same way on
all types of machines. Just keep your manual handy in case you get stumped
about which keys to press to make something happen.
Before you can really start talking to your computer, you'll have to figure out
how to get your messages out of your head and into the machine. Since your
computer can't understand speech, you'll have to let your fingers do the talk
ing. (Or in computer jargon, you must learn to input information from the key
board.)
The letters on all of these computer keyboards are just like the letters on a
typewriter. But each computer has several special keys, or combinations of
keys, that make it work in special ways. This chapter will explain how to use
BASIC Programming for Kids
these keys to type words and numbers (and on some computers, pictures).
It will show you how certain keys can make the computer do some fancy
tricks.
Once you know how to use the keys, then you can get started on learning the
language. The most important key to using the computer, though, isn't on the
keyboard — it's inside you. Your ideas make the computer work. So any time
you have an idea — try it out. That's what your computer is for: it's a machine
for you to think with. Explore, experiment, express yourself— and above all,
have fun! Remember — there's no way you can hurt the computer by typing
on it.
This book is here to help you acquire a few skills you'll need to make your
computer work for you. Use it only as a guide — a starting point for your own
imagination.
To begin, turn to the section that describes your computer's keyboard. The
instructions you'll find there are your jumping-off place for communicating
with your computer.
If you have a computer that is not one of the six listed above, turn to the
Practice section at the end of Chapter 1. When you can answer those questions
for your computer, then go on to Chapter 2.
Apple Keyboard
1. Turn It On.
Make sure everything is plugged in. Find the switch (on the Apple, it's on the
back) and flick it on. Turn on the TV set or monitor. Turn the volume all the
way down if you are using a television set.
If you have a disk drive and are not using a disk, you will probably have to
hold down the key marked 1CTRL I or ICONTROL! (on the left side) and press
the IRESET I key (upper right) in order to get started.
If you are using an Apple He, make sure the ICAPS LOCK I key is pressed
down.
Chapter 1 . All Keyed Up
2. The Cursor.
Somewhere at the left side of the screen you should see a flashing square or box.
This is called the cursor. The cursor shows you where your typing will appear on
the screen.
You should also see a square bracket called a prompt to the left of the cursor.
The square bracket tells you the computer is ready to use the Apple's version of
the BASIC language, known as Applesoft. If you don't see the square bracket,
check your manual to find out how to get into Applesoft.
Press the long key at the bottom of the keyboard. This is called the space bar.
Notice how the space bar moves the cursor one space to the right every time
you press it.
3
BASIC Programming for Kids
puter does not understand you. Your name is not a word in the computer's
language.
Pressing the IRETURN] key means, to the computer, that you have just fin
ished giving it some information. If the computer does not understand the in
formation, it will give back the message SYNTAX ERROR, which means
"wrong word." Any error message is only the computer's way of saying "what
did you mean?" or "I'm confused." It does not mean you've done anything to
hurt the computer.
5. Shift Keys.
Type the number 4 on the top row of keys. Now find a ISHIFT I key. (There are
two ISHIFT I keys. You may use either one.) Put one finger on a ISHIFT I key.
While you are holding down the ISHIFT I key with one hand, type 4 again with
the other hand. What appears on the screen? (You should get a dollar sign: $.)
Keep holding the ISHIFT I key and type the number 5. What appears? (You
should get a percentage sign: %.) Can you explain what the ISHIFT I key does
when you type a number? What do you think you will get if you type the
ISHIFT I key and the number 8? Try it.
Type the letter 0. (It is between I and P in the second row.) Type the zero. (It is
next to the 9 in the top row.)
Chapter 1 - All Keyed Up
Do you see the difference between the computer letter 0 and zero? It's im
portant to remember that the computer makes a zero with a slash through it.
Type the letter I (second row, next to U).
Type the letter L (third row, next to K).
Type the number 1 (top row, next to 2).
Type the word OIL. Type the number 1001. Look hard at these as they ap
pear on the screen. Computers get very confused if you mix up your letters and
numbers. So remember to keep them straight! Any time you mean to type a
number, it must be one of the keys in the top row.
might tell you SYNTAX ERROR even though you can't see any problem in
what you typed. In that case your best bet is just to retype the line.
One way you can use the ICONTROL I key is with X. Typing ICONTROL 1X
will make the computer print a backslash and jump the cursor to the next line.
It tells the computer "Ignore this line." Try this: Type your name and press
IRETURN I. You get a beep and a SYNTAX ERROR. Then type your name,
ICONTROL | X, and IRETURN I. There is no syntax error because the ICONTROL I
Xtold the computer to forget what you just typed. IC0NTR0T"1 Xis useful if you
type something, then change your mind.
Another key you will often use with ICONTROL I is C. ICONTROL I C tells the
computer "Stop!" It's explained in Chapter 3.
ESC E erases everything on the same line to the right of the cursor
ESC F erases everything from the cursor to the end of the screen
IESC I I, J, K, M also put the Apple in Edit mode. If you want to read more
about that, see the section on editing in the back of this book (Appendix III) or
check your Apple manual.
Atari Keyboard
1. Turn It On.
Make sure everything is plugged in and that the BASIC cartridge is in the ma
chine. Find the switch and flick it on. Turn on the TV monitor. Turn the vol
ume all the way down on the TV.
2. The Cursor.
Somewhere at the left side of the screen you should see a little square or box.
This is called the cursor. The cursor shows you where your typing will appear on
the screen.
Press the long key at the bottom of the keyboard. This is called the space bar.
Notice how the space bar moves the cursor one space to the right every time
you press it.
8
Chapter I . All Keyed Up
that the computer does not understand you. Your name is not a word in the
computer's language.
Pressing the IRETURN I key means, to the computer, that you have just fin
ished giving it some information. If the computer does not understand the in
formation, it will give you an ERROR message. An error message does not mean
you have hurt the computer. It only means the computer is confused.
5. Shift Keys.
Type the number 4 on the top row of keys. Now find a ISHIFT I key. (There are
two ISHIFT I keys. You may use either one.) Put one finger on a ISHIFT I key.
While you are holding down the ISHIFT I key with one hand, type 4 again with
the other hand. What appears on the screen? (You should get a dollar sign: $.)
Keep holding the ISHIFT I key and type the number 5. What appears? (You
should get a percentage sign: %.)
Can you explain what the ISHIFT I key does when you type a number? What
do you think you will get if you type the ISHIFT I key and the number 8? Try it.
Type a few letters. Now hold down the ISHIFT I key and press
IDELETE/BACK SI. The whole line should disappear.
Type the letter 0. (It is between I and P in the second row.) Type zero. (It is
next to the 9 in the top row.)
Do you see the difference between the computer letter 0 and zero? On the
BASIC Programming for Kids
Atari the zero has a wiggly line through the center. Many other computers
print a zero with a slash through it, like this 0. That is the way we'll show a zero
in this book, to distinguish it from the letter 0.
Type the letter I (second row, next to U).
Type the letter L (third row, next to K).
Type the number 1 (top row, next to 2).
Type the word OIL. Type the number 1001. Look hard at these as they ap
pear on the screen. Computers get very confused if you mix up your letters and
numbers. So remember to keep them straight! Any time you mean to type a
number, it must be one of the keys in the top row.
10
Chapter I • All Keyed Up
3. A Change of Scene.
Pressing the A key (say "the Atari key") or the ICAPS/LOWRl key changes the
way your typing looks on the screen. The Atari key A reverses your typing
from light to dark and back again.
To insert or erase one character, press IINSERT Ior IDELETE 1with ICTRL 1. To
insert or delete a whole line, press IINSERT I or IDELETE I with ISHIFT I.
11
BASICProgramming for Kids
7. Repeating Function.
Any key on the Atari will start to repeat itself (print over and over again) if you
hold it down for more than half a second.
If you are using an Atari, now skip to the Practice section at the end of Chap
ter 1.
Commodore Keyboard
1. Turn It On.
Make sure everything is plugged in. Find the switch and flick it on. If your
computer is attached to a separate TV set or monitor, turn on the TV. Turn
the volume all the way down on the TV.
2. The Cursor.
Somewhere at the left side of the screen you should see a flashing square or box.
This is called the cursor. The cursor shows you where your typing will appear on
the screen.
Press the long key at the bottom of the keyboard. This is called the space bar.
Notice how the space bar moves the cursor one space to the right every time
you press it.
12
Chapter I . All Keyed Up
5. Shift Keys.
{Note: For the PET only, skip to the special exception below.) Type the number
4 on the top row of keys. Now find a ISHIFT I key. (On most computers there are
two SHIFT keys. You may use either one.) Put one finger on a ISHIFT I key.
While you are holding down the ISHIFT I key with one hand, type 4 again with
the other hand. What appears on the screen? (You should get a dollar sign: $.)
Keep holding the 1SHIFT I key and type the number 5. What appears? (You
should get a percentage sign: %.)
Can you explain what the ISHIFT I key does when you type a number? What
do you think you will get if you type the ISHIFT I key and the number 8? Try it.
Exceptionfor PET: The number keys on the PET are along the right-hand side of
the keyboard. When you type the ISHIFT I key on the PET you get one of the
special graphics symbols that appear on each letter or number key. (For more
about the special graphic symbols on Commodore computers see Special Keys:
Commodore.)
13
BASIC Programming for Kids
Hold down the ISHIFT I key and press ICLR/HOMEl. (If this doesn't work the
first time, press IRETURN! and try again.)
Type the letter 0. (It is between I and P in the second row.) Type zero. (On the
VIC keyboard, it is next to the 9 in the top row. On the PET keyboard it's on
the right, with the numbers.)
Do you see the difference between the computer letter 0 and zero? It's im
portant to remember that the computer makes a zero with a slash through it.
Type the letter I (second row, next to U).
Type the letter L (third row, next to K).
Type the number 1 (top row, next to 2).
Type the word OIL. Type the number 10J01. Look hard at these as they ap
pear on the screen. Computers get very confused if you mix up your letters and
numbers. So remember to keep them straight! Any time you mean to type a
number, it must be one of the keys in the top row. (Or for the PET, it must be
one of the keys on the right-hand side.)
14
Chapter I . All Keyed Up
side of the front of the letter keys. Experiment with using the ISHIFT I key to
type other characters.
3. Shift Lock.
Push the ISHIFT/LOCK I key and let go. (It should stay pressed down even
though you are not holding it.)
Type 12 3. What do you get? Type Q W E. What appears? Notice that the
ISHIFT/LOCK I key works just like the ISHIFT I key, but you don't have to keep
holding it down. To release the ISHIFT/LOCK I key, tap it again lightly.
15
BASIC Programming for Kids
6. Changing Colors.
If you have a color TV, experiment with changing colors by typing the ICTRL I
key and the keys from 1 through 8. You must hold down the ICTRL I key while
you type the color key. If you have a black and white TV, you will notice a
slight change in the shading, but of course you won't be able to get different
colors on the screen. (Incidentally, if you're wondering about the color CYN,
that stands for cyan, which is a shade of blue.)
Hold down the ICTRL Ikey and type 9. That gives you REVERSE ON (which
means you get white letters on a dark background). To go back to normal, hold
down ICTRL I and type 0.
7. Repeating Keys.
Some of the keys on the VIC are repeating keys. If you press them and keep
holding them down, they will keep on going. For example, the space bar, the
IINST/DEL Land the two cursor arrow keys can repeat. Hold any of them down
and see what happens.
8. Function Keys.
Don't worry about the four keys labeled Fl through F8 on the right side of the
keyboard. In some games or other types of programs they are given special
functions to perform, but you won't need to use them for anything in this book.
16
Chapter 1 . All Keyed Up
9. Restore Key.
The IRESTORE I key is used with IRUN/STOPl to make the computer stop a
program. This is demonstrated in Chapter 4, Section 8.
Special Notefor PET computers: You can get graphics characters on the PET with
the ISHIFT I key (Section 1 above) and you can move the cursor with the cursor
arrow keys (Section 5). The PET does not have the other special keys of the
VIC keyboard, and it does not have color. You can reverse the letters (from
light to dark) using the IQFF/RVS"! key. To go back to normal press ISHIFT I
with IQFF/RVS I.
If you have a "business style" keyboard (found on CBM computers) the nor
mal mode of typing is upper and lower case. To get the graphics characters in
stead of lower case letters type this: POKE 59468, 12 IRETURN I. When you
want to go back to upper and lower case type POKE 59468, 14 IRETURNl.
If you are using a Commodore, now skip to the Practice section at the end of
Chapter 1.
TRS-80 Keyboard
1. Turn It On.
Make sure everything is plugged in. Find the switch and flick it on. If your
computer is attached to a separate TV set or monitor, turn on the TV. Turn
the volume all the way down on the TV.
You are ready to start typing when you see the word READY (or, on the
Color Computer, OK) on the screen. If you don't see READY, press the IENTER I
key until it appears.
If you have a TRS-80 with a disk drive and you can't get the computer
going, try this: Hold down the IBREAK I key and press the orange reset key on
the right side of the keyboard. Do this several times if necessary. You may also
need to adjust the tuning on the TV screen with the controls under the left side
of the keyboard.
17
BASIC Programming for Kids
2. The Cursor.
Somewhere at the left side of the screen you should see a little square or box (or
on the Model I, just a little line). This is called the cursor. The cursor shows you
where your typing will appear on the screen.
You should also see a prompt to the left of the cursor. It looks like a sideways
V: >. The prompt tells you when you're beginning a new line.
Press the long key at the bottom of the keyboard. This is called the space bar.
Notice how the space bar moves the cursor one space to the right every time
you press it.
18
Chapter 1 * All Keyed Up
word." An error message does not mean you have hurt the computer. It only
means that the computer is confused.
5. Shift Keys.
Type the number 4 on the top row of keys. Now find a ISHIFT I key. (On most
computers there are two ISHIFT I keys. You may use either one.) Put one finger
on a ISHIFT I key. While you are holding down the ISHIFT I key with one hand,
type 4 again with the other hand. What appears on the screen? (You should get
a dollar sign: $.) Keep holding the ISHIFT I key and type the number 5. What
appears? (You should get a percentage sign: %.)
Can you explain what the ISHIFT I key does when you type a number? What
do you think you will get if you type the ISHIFT I key and the number 8? Try it.
Now type a whole line of letters. Then hold ISHIFT I and press the left arrow
key. Notice how ISHIFT I F^l erases the whole line.
Type the letter 0. (It is between I and P in the second row.) Type zero. (It is
next to the 9 in the top row.)
Do you see the difference between the computer letter 0 and zero? It's im
portant to remember that the computer makes a zero with a slash through it.
Type the letter I (second row, next to U).
Type the letter L (third row, next to K).
Type the number 1 (top row, next to 2).
19
BASIC Programming for Kids
Type the word OIL. Type the number 10J01. Look hard at these as they ap
pear on the screen. Computers get very confused if you mix up your letters and
numbers. So remember to keep them straight! Any time you mean to type a
number, it must be one of the keys in the top row. (Or if your computer has a
special set of number keys like a calculator, those are okay too.)
arrow key \T\ — instead of doing what you might expect — does not move the
cursor up. Instead, it prints something on the screen. Depending on which
model TRS-80 you have it may print an arrow f, or it may print a square
bracket [. (Actually, this is an exponent key. You use this when you want to
type something like 10 to the third power. Math books sometimes write this as
103. On the computer you type it 10|3.)
One other combination of keys you should know about is ISHIFT I 0. On some
models of the TRS-80, typing those two together will put you in lower-case
mode. When that happens, every letter you type will come out as a small
letter. If you want a capital letter, you have to use the ISHIFT I key (just like
a regular typewriter.) To go back to normal upper-case mode press ISHIFT I 0
again.
On the TRS-80 Color Computer ISHIFT I0 makes the letters on the screen go
into reverse mode printing — instead of black on green, they become green on
black. In reverse mode the computer will not carry out your commands the way
it does normally — so you should stick to the regular letters for writing your
programs. Typing ISHIFT I0 again will put the computer back in normal mode.
If you are using a TRS-80, now skip to the Practice section at the end of
Chapter 1.
Make sure everything is plugged in. Find the switch and flick it on. Turn on the
TV set, or monitor, and follow the instructions on the screen to start up TI
BASIC.
21
BASIC Programming for Kids
2. The Cursor.
At the bottom left side of the screen you should see a little flashing box. This is
called the cursor. The cursor shows you where your typing will appear on the
screen.
You should also see a prompt to the left of the cursor. It looks like a sideways
V: >. The prompt tells you when you're beginning a new line.
Press the long key at the bottom of the keyboard. This is called the space bar.
Notice how the space bar moves the cursor one space to the right every time
you press it.
4. Fixing Mistakes.
With your right hand hold down the key marked IFCTNI at the lower right.
While holding IFCTN I, press S (the key with a left arrow |<—Ion its front). No
tice how the cursor backs up. When you want to change a letter, use IFCTN I S
to move the cursor to the mistake. Then type the correction. If you want to
erase a letter instead of changing it, move the cursor back and then erase with
the space bar. When you have made the correction, you can use IFCTN I D (the
right arrow) to move the cursor back to the right without erasing anything.
Put the cursor back at the end of your name. Now hold the IFCTN I key and
press 3. Your name (and anything else on that line) disappears! IFCTN I 3 erases
a whole line of typing with two keystrokes.
22
Chapter I • All Keyed Up
Practice. Type this word: CUMPUTER. Do you see the spelling mistake? Back
up the cursor and correct the word to spell COMPUTER.
6. Shift Keys.
Type the number 4 on the top row of keys. Now find a ISHIFT I key. Put one
finger on a ISHIFT I key. While you are holding down the ISHIFT I key with one
hand, type 4 again with the other hand. What appears on the screen? (You
should get a dollar sign: $.) Keep holding the ISHIFT I key and type the number
5. What appears? (You should get a percentage sign: %.)
Can you explain what the ISHIFT I key does when you type a number? What
do you think you will get if you type the ISHIFT I key and the number 8? Try it.
Note: If the IALPHA LOCrTI key is not pressed, holding the ISHIFT I key will also
give you large capitals when you type letters.
First type several lines of anything. Then press IENTER I (and just ignore the
INCORRECT STATEMENT), type the words CALL CLEAR and press the
IENTER I key. Presto — it's all gone! (If this doesn't work the first time, press
IENTER I and try again.)
Type the letter 0. (It is between I and P in the second row.) Type zero. (It is
next to the 9 in the top row.)
Do you see the difference between the computer letter 0 and zero? Notice
that on the TI the zero is more oval-shaped and the letter 0 is a rectangle. (On
most other computers, however, a zero appears on the screen with a slash
through it, the way it looks on the TI keyboard.)
Type the letter I (second row, next to U).
Type the letter L (third row, next to K).
Type the number 1 (top row, next to 2).
Type the word OIL. Type the number 1J001. Look hard at these as they ap
pear on the screen. Computers get very confused if you mix up your letters and
numbers. So remember to keep them straight! Any time you mean to type a
number, it must be one of the keys in the top row.
9. More Functions.
You've seen how to use the IFCTN I (for FUNCTION) key to move the cursor
(with S and D) and to erase a whole line (with 3). The IFCTN I key also allows
you to type any of the special symbols on the front of some of the letter keys.
(One that's very important is the quotation mark on the front of the P.) Ex
periment with typing IFCTN I and the letter keys to see those symbols.
IFCTN I 1 and IFCTN I 2 are used in editing or correcting. For an explanation
of how to use these, see Appendix III: Editing Your Mistakes (at the back of the
book).
24
Chapter I . All Keyed Up
You won't need to use most of the other special functions any time soon. But
one to watch out for is IFCTN I|T]. Press that now to see what happens. If you
ever press IFCTN I [T] by mistake (and it isn't hard to do), you'll wipe out
everything that is in the computer's memory at that moment. So be careful!
ICTRL 1stands for CONTROL, but the special functions of this key are for ad
vanced programmers. You won't need to worry about them now.
If you are using a Texas Instruments computer, now skip to the Practice sec
tion at the end of Chapter 1.
2. The Cursor.
The cursor is a small box that shows you where your typing will appear on the
screen. When you first turn on the Timex Sinclair, the cursor appears with
the letter K inside it. This stands for Keyword. When you see the K cursor,
your keyboard will not type letters like a typewriter. Instead, the next time you
press a letter, you will get the keyword that is printed in small letters just
above the letter. You'll learn more about what those keywords mean in later
chapters.
For now we're just experimenting with the keyboard, so press P. The word
PRINT should appear on the screen. Notice that the K in the cursor has now
changed into an L. That stands for Letter, and means that the next time you
press a key the letter (not the keyword) will appear on the screen. To check this,
type your first name. Press the space key and type your last name.
25
BASIC Programming for Kids
26
Chapter 1 . All Keyed Up
press V. This is the CLS (for Clear Screen) key — and it erases anything on the
TV screen. (The 0/0 that appears at the bottom of the screen is a code mes
sage. When the first number is zero, it means everything is okay. You can look
up the meaning of those code messages in the back of your Timex Sinclair
manual.)
Be sure you notice the difference between the computer letter 0 and 0. It's im
portant to remember that the computer makes a zero with a slash through it.
Also, don't get the number 1 and the letter I confused. They may look alike
when you write them, but they mean two very different things to the computer.
Any time you mean to type a number, it must be one of the keys in the top row.
8. Printing Graphics.
To print the graphics symbols on the keys, do this: Hold the 1SHIFT I key and
press 9. The cursor letter should change to G. Then keep holding the ISHIFT I
key and type QWERTY. A checkered pattern should appear. To get back to
normal, press ISHIFT I 9 again.
27
BASIC Programming for Kids
9. Function Keys.
To print the words that appear under each key, hold ISHIFT I and press
IENTER |. This changes the cursor to F (for Function). Now when you press P,
for instance, you will see the word TAB appear. We'll be using a couple of these
functions later in programs, but you don't have to worry about them for now.
Practice 1
After you have completed Chapter 1, check yourself with these questions.
1. What key (or keys) do you push to erase a letter you have just typed, or to
correct a mistake?
2. What is the cursor?
3. What do you type to get an exclamation point (!)?
4. How do you clear the screen (that is, erase everything on the screen)?
5. What does the long key at the bottom of the keyboard do?
6. What are the words the computer tells you when it doesn't understand you?
7. How does the computer make a zero?
Answers to Practice 1
28
Chapter I . All Keyed Up
3. On most computers, ISHIFT I and 1. (Exceptions: On the PET, you type only
the key with the ! on it. On the Timex Sinclair there is no exclamation
point.)
4. Apple —fESCllSHIFTI @
Atari — I SHIFT I I CLEAR/< I
Commodore —I SHIFT I ICLR/HOMEI
TRS-80 — ICLEAR I
Texas Instruments — CALL CLEAR
Timex Sinclair — V (the CLS key)
5. It makes a space.
6. SYNTAX ERROR (or the abbreviation). (Exception: The TI says INCOR
RECT STATEMENT.)
7. 0 with a slash through it, or 0 (except on the Atari and TI).
29
2.
1,2,3
Run!
To talk to your computer you must learn to speak its language. The language
your computer uses is called BASIC. (If you like trivia, you may want to know
that BASIC stands for Beginners' All-purpose Symbolic Instruction Code.)
1. Printing in BASIC.
One of the most important words in the BASIC language is PRINT. Let's see
what happens when you tell the computer to PRINT some different things.
Type these commands into the computer, and see what the computer prints.
(Ignore it if your computer prints READY or OK or some such message before
the cursor. That just means it's ready for the next instruction.)
Note: Where you see IRETURNl that means press the IRETURN] key. If you have
a TRS-80, Texas Instruments, or Timex Sinclair computer, press IENTER 1
where you see IRETURNl.
1. You type PRINT 82 IRETURNl What does the computer print?
2. You type PRINT 8 + 2 IRETURNl What does the computer print?
3. You type PRINT 8-2 IRETURNl What does the computer print?
4. You type PRINT 8*2 IRETURN What does the computer print?
5. You type PRINT 8/2 IRETURNl What does the computer print?
30
Chapter 2 . I, 2, 3 —Run!
6. You type PRINT HELLO IRETURNl What does the computer print?
7. You type PRINT "HELLO" IRETURNl What does the computer print?
8. You type PRINT "8 + 2" IRETURNl What does the computer print?
(If you typed everything correctly, the computer should have printed: 82,
10, 6, 16, 4, 0, HELLO, and 8 + 2.)
Notice that the * symbol means "times" and the / sign means "divided by"
to the computer. Do not try to type X for multiplication. The computer won't
know what you mean.
When you tell the computer to print a math problem, it automatically prints
the answer, unless you type the problem inside quotation marks.
Finally, notice that when you want the computer to print a word, you must
put the word inside quotation marks.
It's very important when you type commands into the computer to do them
in exactly the right way. Computers are pretty dumb about figuring out what
you mean. If you haven't yet made a mistake in your typing, let's try it to see
what the computer will do.
Suppose your finger slips and you type PRIN 82. If you remember Chapter 1,
you may be able to predict what the computer will tell you when it doesn't un
derstand you. Try it and see.
Did you get that error message again? Any time you see that, just look back
over what you typed and see if you can find your mistake.
31
BASIC Programming for Kids
Before you start any program you should type the word NEW. This tells the
computer you are beginning a new program.
Type this program exactly as it appears. Be sure you type the numbers!
Remember: On the TRS-80, TI, or Timex Sinclair you will use IENTER I instead
of I RETURN I.
NEW I RETURN I
1 PRINT "I" IRETURNl
2 PRINT "LOVE" RETURN
3 PRINT "YOU" IRETURNl
32
Chapter 2 . I, 2,3 —Run!
and the computer should print the whole program, starting with line 1, like
this:
LOVE
YOU
DO YOU
LOVE ME?
6. Mixing Up Numbers.
Let's see what happens when you mix up the order of your directions.
Note: From now on we will not tell you to type IRETURNl at the end of a line.
You will have to remember to press the IRETURNl or IENTER Ikey at the end of
every line.
NEW
2 PRINT "MY"
1 PRINT "KISS"
4 PRINT "DOG"
33
BASIC Programming for Kids
3 PRINT "SMELLY"
RUN
What does the computer print? Notice that the computer printed the words in
the order of the numbers, not in the order you typed them.
To see the way the computer stored or remembered the program, type LIST.
The computer should print:
1 PRINT "KISS"
2 PRINT "MY"
3 PRINT "SMELLY"
4 PRINT "DOG"
LIST is another very important word in the BASIC language. To the com
puter, LIST means: "Show your program on the screen" or "Print the whole list
of instructions for this program in order."
Remember that RUN will make the computer actually do the instructions in
the program. LIST will make the computer show you what those instructions
are. To make sure you understand the difference between these two words, type
RUN IRETURNl and then LIST IRETURNl several times. Notice how the com
puter responds to each command.
If you want to list just part of the program, type LIST, followed by the line
number(s) you want to see. For example, type
LIST 3 IRETURNl
and the computer should print
3 PRINT "SMELLY"
(The Timex Sinclair will print line 3, and every line after that.)
8. Making Changes.
If you decide to change part of your program, you don't have to start all over
again. All you need to do is retype the line or lines you want to change. Sup-
34
Chapter 2 • /, 2, 3 —Run!
pose you decide you'd rather wash your dog than kiss him. Change line 1 by
typing this:
1 PRINT "WASH"
RUN
and the computer should print
WASH
MY
SMELLY
DOG
When you list the program, you will see that the computer has erased your old
line 1 from the program and put the new line in its place.
If you want to erase a whole line, just type the line number and IRETURNl (or
IENTER f). Our poor dog gets insulted when we call him smelly, so let's take that
line out. Type
3 [RETURN I
RUN
and the computer should print
WASH
MY
DOG
And now when you type LIST, you should see
1 PRINT "WASH"
2 PRINT "MY"
4 PRINT "DOG"
9. Number by Tens.
Tip: Instead of numbering programs starting 1, 2, 3, 4, etc., programmers
usually go by tens: 10, 20, 30, 40, etc. That makes it easy to go back and insert
lines you might think of later.
Type this
NEW
10 PRINT "I AM"
35
BASIC Programming for Kids
36
Chapter 2 - I, 2, 3—Run!
it is like telling the computer: "Throw away everything in all your envelopes,
and get ready to insert some new messages." (Turning off the computer also
empties out all the messages.)
37
BASIC Programming for Kids
10 PRNT "HI"
RUN
and the screen will show
?SN ERROR IN 10 (or SYNTAX ERROR IN 10)
READY
10
Line 10 is now ready and waiting for you to edit it. If you want to use the
Edit mode, read Appendix III. If you don't want to use the Edit mode (and
for a simple mistake like this it's probably not worth it), just press IENTER |.
The computer will again show you the whole line with the mistake. Press
IENTER I again and retype the line correctly.
•*• If you have an Atari, it will tell you that you've made a typing mistake as
soon as you press IRETURN". It will do that by reprinting the line with the
word ERROR after the line number, and highlighting the mistake. You can
get things back on track by retyping the line correctly. (See Appendix III for
more details.)
38
Chapter 2 . /, 2,3 —Run!
Practice 2
In Chapter 2 you learned some important words. Some of these words are: pro
gram, BASIC, PRINT, NEW, RUN, LIST. Look back over the chapter and an
swer these questions.
1. What word do you always type in when you want to erase an old program
and start a new one?
2. What key do you press to tell the computer you have finished typing some
thing?
3. What word tells the computer to show you the instructions (the numbered
lines) for the program it is currently working on?
4. What word tells the computer to start doing the instructions in the pro
gram?
5. What will the computer print if you type PRINT 5 + 5?
6. What will the computer print if you type PRINT 5*5?
7. What will the computer print if you type this program?
3 PRINT "ME ON"
1 PRINT "COMPUTERS"
2 PRINT "TURN"
RUN
8. What will the computer print when you run this program?
NEW
10 PRINT 2 * 1
20 PRINT "GOOD"
30 PRINT 8/4
40 PRINT "B"
50 PRINT 6 - 2
60 PRINT "GOT"
70 PRINT 5 + 5
(Read the above message fast, and you'll know what we hope you think
about the computer lessons.)
39
BASIC Programming for Kids
9. What is the name of the language you are learning for your computer?
10. What do we call a set of numbered instructions to the computer?
11. What will the computer print if you type PRIMT 42?
Answers to Practice 2
1. NEW
2. IRETURNl (or IENTER!)
3. LIST
4. RUN
5. 10
6. 25
7. COMPUTERS
TURN
ME ON
8. 2
GOOD
2
B
4
GOT
10
9. BASIC
10. program
11. SYNTAX ERROR (or the equivalent on your computer).
The TI will print INCORRECT STATEMENT.
On the Timex Sinclair it is impossible to type PRIMT.
40
3
Drive the
Computer
Loopy
1. A Nonstop Program.
Type this program:
NEW
10 PRINT "XXXXXX"
20 GOTO 10
RUN
What happens? You should see the computer print an endless string of X's
along the left side of the screen. (Or if you're feeling friendly toward your com
puter, you might want to think that it is sending you a string of kisses.)
3. How It Works.
4. A Larger Loop.
You can have as many program lines as you want included in your loop. For
example:
NEW A diagram of this program
10 PRINT "THIS PROGRAM" would look like this:
20 PRINT "IS DRIVING" ^10>
30 PRINT "ME LOOPY!" ^20
40 GOTO 10
RUN
When the program runs off the top of the screen like that and keeps going,
we say it is scrolling. There are certain things you can do on some computers to
interrupt or slow down the scrolling without breaking off the program. Here's a
list:
Hold down the ICTRL I key and type S. This will "freeze" the screen (stop
the scrolling). When you press any key, the scrolling will begin where it left
off.
42
Chapter 3 • Drive the Computer Loopy
A Hold down the ICTRL I key and type 1 to interrupt the scrolling. Typing
ICTRL | 1 again will restart it.
TRS Hold down the ISHIFT I key and press j@J. Hit any key to continue.
&VIC There is no key on the Commodore to freeze the screen. However, on the
VIC or 64 you can slow down the scrolling to make it easier to read by hold
ing down the ICTRL Ikey. As soon as you let up the ICTRL Ikey it will speed
up again.
C*PET On the PET you must press IRUN/STOP |. If you want to keep going from
the point where you stopped, type CONT (for CONTinue). (This will also
work on the VIC.)
TI The TI scrolls so slowly you don't need to freeze it.
T/S The Timex Sinclair does not scroll; it stops when the screen is full.
43
BASIC Programmingfor Kids
When you run this program, you'll see the whole screen covered with X's.
The semicolon makes the computer print all the X's right next to each other.
You might want to experiment with some different keys on yourcomputer to
see what kinds of patterns you can make on the screen with programs like this.
For example, change line 10 to:
10 PRINT "XO";
or,
10 PRINT "<*>";
Practice. Write a program using the semicolon to fill up the screen with your
name.
6. Jumping Ahead.
So far we have used GOTO to make the computer go backward in a program.
You can also use GOTO to make the computer go forward and skip over lines in
a program.
Type in the program we did at the end of Chapter 2:
NEW
10 PRINT "I AM"
20 PRINT "A BOY"
Remember how we added line 15 to change that program for girls? Type in
that line 15. (Ifyou've forgotten what it was, turn back to Chapter 2 and look.)
Run the program. It should print:
I AM
NOT
A BOY
44
Chapter 3 • Drive the Computer Loopy
Practice 3
1. Think up a good slogan for your school. Write a program that will make the
computer print your slogan over and over again.
2. What will the computer print if you type in this program and run it? Think
before you answer.
10 PRINT "THIN";
20 PRINT "KING"
3. Look at this program:
10 PRINT "SMART PEOPLE"
20 PRINT "NEVER"
30 PRINT "MAKE"
40 PRINT "MISTAKES"
If you run this program now it will print
SMART PEOPLE
NEVER
MAKE
MISTAKES
What line could you add to this program to make it print
SMART PEOPLE
MAKE
MISTAKES
(Hint: Look at the last program in Chapter 3.)
45
BASIC Programming for Kids
Answers to Practice 3
1. (This is a sample program. You will have your own slogan in line 10. Or
maybe your slogan will take more than one line.)
10 PRINT "DON'T BE A FOOL —COME TO SCHOOL"
20 GOTO 10
2. THINKING
3. You would add line 15 (or any other number between 10 and 20):
15 GOTO 30
46
4
Your
Number,
Please?
47
BASIC Programming for Kids
PRINT A
and the computer prints 0.
Now type
A = 7 (or LET A = 7)
PRINT A
You have changed A from a zero to a 7. You can change it again, if you
want, into any number you choose. Remember, you're in charge here. Pick
your favorite number and tell the computer that A equals that number.
Note: On most computers the use of the word LET in such statements is op
tional. The computer will do exactly the same thing whether you say H = 5 or
LET H = 5. On the Timex Sinclair, however, you must use the LET statement.
48
Chapter 4 • Your Number, Please?
but the envelopes with letters on the outside hold numbers. So when the com
puter finds the label A in the program, it goes to the "envelope" marked A,
either to put something in or take something out.
49
BASIC Programming for Kids
Variables are very useful in a program when combined with the BASIC word
INPUT. The INPUT statement makes the computer stop the program until the
person at the keyboard types something and hits IRETURNl or IENTER I.
To see how INPUT works, type this one-line program:
NEW
10 INPUT N
RUN
and you should see a question mark appear, followed by the cursor. (On the
Timex Sinclair, you will see only the cursor, with an L inside it.) That means
the computer is waiting for you to tell it what N equals. It wants you to INPUT
a number to stand for N. (Or, it wants you to PUT a number IN the N "enve
lope.") Type in any number, and press IRETURNl or IENTER I. Then type
PRINT N, and the computer should print the number you just typed in. By
using INPUT you can make your programs do something different every time
you run them.
The word INPUT in a program means "The computer needs some informa
tion here." But you must be sure that your program explains what kind of in
formation it wants. So whenever you write an INPUT line, you should put a
PRINT line before it. For example, you might begin this program by saying
5 PRINT 'TYPE A NUMBER."
Now when you run the program it becomes clear what you want the person at
the keyboard to type.
Note: On some computers (for example, the Apple and TRS-80) you don't have
to put your PRINTstatement in a separate line when you use INPUT. Instead of
saying this:
5 PRINT "TYPE A NUMBER."
10 INPUT N
you can say
10 INPUT "TYPE A NUMBER"; N
But in our programs we will always do it the first way and keep PRINT and
INPUT in separate lines.
50
Chapter 4 • Your Number, Please?
51
BASICProgramming for Kids
6 IS A NICE NUMBER.
TYPE ANOTHER NUMBER
?5
ICAN ADD THESE. THE TOTAL IS
11
Now you run it several times, with different numbers each time.
You may pick any letter in the alphabet to stand for your variable numbers.
The program would work exactly the same way if you used P and Q instead of
X and N.
There are two important things to watch out for with variables:
1. Use a different letter for each different variable.
2. Always make sure the variables are outside the quotation marks.
Think about the "envelope idea" when you are using variables in different
ways in your programs:
A = 5 means "Put the number 5 into the envelope marked A."
PRINT A means "Show me what's in the Aenvelope."
INPUT A means "Hold the A envelope open until someone puts a number
in it."
programs in Chapter 3. But on some computers you must push an extra key to
break in when your program is stopped at an INPUT line, waiting for you to
answer.
Here's how to break into your program when it's stopped at an INPUT line:
« Press I CTRL I C and IRETURNl.
O PET only: Press IRUN/STOP [ and IRETURN I.
O VIC or 64: With your left hand hold down the |RUN/STOP"| key while with
your right hand you press the IRESTORE | key.
TRS A Press the IBREAK I key.
TI Hold down the IFCTN I key and press 4.
T/S Hold down ISHIFT I and press A. (This equals STOP.)
It would be easy to change this program to do other kinds of problems be
sides addition. For example, you could make it multiply the two numbers N
and X together, instead of adding them. You would only have to change two
lines. (Remember, the times sign on the computer is *, not X. And if you want
to divide, this sign / means "divided by.")
List the program, figure out which two lines you would change, and try it.
53
BASIC Programming for Kids
About INPUT: Remember, INPUT puts a question mark on the screen and
makes the computer stop and wait for you. When you see the question mark,
followed by the cursor, the computer is waiting for INPUT in a program. It is
not ready for you to type RUN, LIST, NEW, a program line, or any other BASIC
commands. When you see the question mark, you must either answer the ques
tion or change the subject. And to change the subject, you must first BREAK
out of the program, as described above. It's like being on the telephone —
when you're in the middle of a call, you can't make another one until you first
hang up the phone. Once you "call up" a program by typing RUN, you must
either finish the program or "hang up" on it with BREAK, before you do any
thing else.
You can use the computer to figure out all kinds of math problems. Sometimes
the easiest way to do that is to use a formula in your program. For instance,
since there are 12 inches in one foot, you could write a formula like this:
I = F * 12 or LET I = F * 12
I, of course, stands for the number of inches. Then you can use that formula in
a program to change feet into inches:
NEW
10 PRINT "THIS PROGRAM WILL CHANGE FEET INTO INCHES."
20 PRINT "TYPE THE NUMBER OF FEET."
30 INPUT F
40 I = F * 12
50 PRINT F; "FEET = " ;l; " INCHES"
In line 40 you taught the computer the rule for changing feet to inches. That
line makes the computer do the math "in its head." After line 40 the computer
will remember what I equals and put in that number when you tell it to print I
in line 50. (Remember, on the Timex Sinclair, line 40 must say LET I = F * 12.)
Look carefully at line 50. Notice how the quotation marks are put around
54
Chapter 4 • Your Number, Please?
the words and the equal sign, but the variables F and I are outside the quotes.
Because the letters are outside the quotes the computer automatically recog
nizes them as variables and therefore prints the numbers that they stand for.
Run that program a few times putting in different numbers for INPUT.
55
BASICProgramming for Kids
• Then teach the computer the rule for figuring out the total weight, so it can
do the math. In other words, just type the formula into the program.
• Since the computer now knows what numbers to put in for K and P, it has
already figured out what W equals. In the last line it will print W, or the total
weight of the whole class.
You might want to try to write the program yourself before you look at our
example.
If you are in fifth grade, your program might look something like this:
NEW
10 P = 71 (or 10 LET P = 71)
20 PRINT "HOW MANY KIDS ARE IN YOUR CLASS?"
30 INPUT K
40 W = K * P (or 40 LET W = K * P)
50 PRINT "ALL TOGETHER THEY WOULD WEIGH "; W; " POUNDS"
If you wanted to write that program for sixth grade you would only have to
change one short line. Which line is it and how would you change it? (Check
your answer in the Answer section for this chapter.)
Practice 4
1. Write a program that will add any three numbers together. In other words,
the program will look something like the example of the addition program
in Chapter 4, but the computer will have to ask the person to type in three
different numbers. So the program will have three INPUT lines and three
different variables.
2. Write a program to make the computer figure out about how many hours
you sleep in one year. Here are the steps the program must go through:
• First the computer will have to find out how many hours you sleep every
night. (That will take a PRINT line for the question and an INPUT line for
the answer — using H to stand for the number.)
56
Chapter 4 • Your Number, Please?
• Then you tell the computer the formula it will need to figure out the answer.
The total number of hours slept (S) equals the hours slept per night (H)
times the number of days in a year (365). So the formula is S = H * 365.
• Finally the computer prints the total number of hours slept per year (S).
When you have translated this into BASIC, your finished program should
look something like the example in Section 11 of Chapter 4 (Feet into
Inches).
3. Add some lines to the program in Problem 2 to make the computer figure
out how many days old you were on your last birthday. To make it simple,
we won't count leap years, so the formula would be D (for total number of
days) = Y (number of years old) * 365.
First, you will need to think about the steps involved, as we did in Problem
2. Then put those steps into your BASIC program.
4. Add to the program in Problems 2 and 3, to tell you how many hours you
have slept in your whole life. The formula would be T (total hours slept) = S
* y
Answers to Practice 4
57
BASIC Programming for Kids
Note: In all answers, the letters you choose for the variables and the wording in
your PRINT statements may be slightly different from ours. These are only
sample programs.
1. 10 PRINT "TYPE A NUMBER."
20 INPUT A
30 PRINT "TYPE ANOTHER NUMBER."
40 INPUT B
50 PRINT "TYPE ONE MORE NUMBER."
60 INPUT C
70 PRINT "THE SUM OF THOSE NUMBERS IS "; A + B + C
58
Chapter 4 • Your Number, Please?
NUMBER OF WALKS
?4
NUMBER OF HITS
? 8
THE BATTING AVERAGE IS .307692308
Of course, we don't usually see batting averages that look like that number.
Just drop off everything after the first three numbers to make the batting aver
age .307. (Actually, if you know about rounding off, you'll realize it should be
.308.)
59
5.
All Tied
Up in
Strings
In Chapter 4 you learned how to write programs where the variables were
numbers. You will probably have even more fun writing programs where the
variables can be words. These programs will let the computer seem to carry on
a real conversation with you.
1. Let's Experiment.
When your variable was a number, you typed something like this:
A=6 (or LET A=6)
Remember: You must use LET on the Timex Sinclair. On others, it's optional.
What happens if you try to make your variable A stand for a word? Let's see.
Type this:
A= HELLO (or LET A=HELLO)
PRINT A
and your computer prints a zero. This may remind you of something in Chap
ter 2. Remember, to make the computer print a word, you had to type PRINT
"HELLO". When you didn't use the quotes, the computer gave you a zero.
Well, those quotation marks worked before. Maybe they will again. Try this:
A="HELLO"
60
Chapter 5 • All Tied Up in Strings
No, that didn't work either. What did the computer tell you this time?
That error message means you tried to put in a word where the computer
wanted a number. In BASIC a plain letter (or group of letters) can only be used
to stand for a number.
To tell the computer to expect a word for a variable, you use a letter (any let
ter in the alphabet), plus a dollar sign ($). For example — A$.
The computer word for this $ symbol is string. So when you see A$ in a pro
gram, you say "A-string." Actually, a string can be any combination of letters,
numbers, or graphics symbols.
A$fB$»C$, etc., are examples of labels for a third type of "envelope" in the
computer's memory. Remember, the "envelopes" labeled with numbers con
tain program lines. The "envelopes" labeled with plain letters contain num
bers. And the "envelopes" labeled with a letter plus a dollar sign may contain
anything you can type on the keyboard.
61
BASIC Programming for Kids
say DIM A$(lJ0), you can make A$=llHELLOM, because HELLO has less than 10
letters. You can't make A$="HELL0 THERE" because that adds up to 11 let
ters and spaces.) The biggest your DIM number can be is 255.
Now, if you spelled everything right and remembered your quotation marks,
you've finally made the computer print HELLO.
Try this:
B$="SAM" (or LET B$="SAM")
PRINT A$;B$
A Remember for the Atari you must first type
DIM B$(10)
Did your computer print HELLOSAM—all one word? Here's one way you
can make it space:
PRINT A$,B$
The comma tells the computer to space over and start a new column. (The
number of spaces in the column and the number of columns on one line depend
on what kind of computer you have.)
If you just want to leave one space between words, you must put your space
inside the quotation marks, like this:
B$= " SAM"
Now type
PRINT A$;B$
Practice this. Try making B$ equal your name (unless your name is Sam, of
course, and you've already done it). Then make the computer say hello to you.
Next use some other letters to create new string variables. Tell the computer to
print your variables with commas between them, like this:
PRINT A$,B$,C$,D$
That way you'll find out where the columns start on your computer.
62
Chapter 5 • All Tied Up in Strings
NEW
10 PRINT "WHAT IS YOUR NAME?"
20 INPUT N$
30 PRINT "NICE TO MEET YOU,";N$
RUN
A On the Atari you must begin your program with a DIM statement in line 1, like
this: 1 DIM N$(20)
Notice (in line 30) that N$ must be outside the quotation marks. Variables
can never be put inside quotation marks.
Run this program several times. Give the computer some different answers
when it asks you "What is your name?"
4. A Colorful Example.
Here's another program using a string variable:
NEW
10 PRINT "WHAT IS YOUR FAVORITE COLOR?"
20 INPUT C$
30 PRINT "ONLY THE SMARTEST PEOPLE LIKE ";C$
A Atari only, add this line: 1 DIM C$(20)
Run this program several times. Try giving the computer a silly answer when
it asks for your favorite color. You'll find out that the computer doesn't know
whether you actually typed in a color for an input. No one has taught the
computer what is or is not a color, so it will keep right on with the program
even if you tell it that your favorite color is spaghetti.
5. Triple Strings.
You can put lots of string variables in one program. Here's an example:
NEW
10 PRINT "WHAT IS YOUR NAME?"
20 INPUT N$
63
BASIC Programming for Kids
6. Try It Yourself.
See if you can write a program like this:
First, the computer asks you what school you go to. (This will be a PRINT
line.)
Second, it waits for your answer, and gives it a string variable code name.
(This will be an INPUT line.)
Third, the computer prints is the best! (In the blank the computer
would print the name of your school when you run the program. In other
words, the blank is the string variable.)
The last line of the program would make the computer print over and over
again that your school is the best. (This will be a GOTO line, going back to the
PRINT line just before.)
After you've written your own program, check it against our example:
64
Chapter 5 • All Tied Up in Strings
NEW
10 INPUT "WHAT SCHOOL DO YOU GO TO?"
20 INPUT S$
30 PRINT S$; " IS THE BEST!";
40 GOTO 30
A Atari, add: 1 DIM S$(25)
Note: The semicolon at the end of line 30 is optional.
Practice 5
1. Write a program with two string variables that would make the computer
print something like this:
PLEASE TYPE YOUR NAME
? JENNY
NAME ONE PART OF YOUR BODY
? NOSE
JENNY HAS A BEAUTIFUL NOSE.
(The words JENNY and NOSE would be typed in by the person running the
program. So if John ran the program, he could type in JOHN and BIG TOE,
and the computer would tell him, "JOHN HAS A BEAUTIFUL BIG TOE.")
Here are the steps the program must go through:
First, the computer asks you to type your name.
Second, it waits for your answer, and gives your answer a string variable code.
Third, it asks you to name one part of your body.
Fourth, it waits for that answer and gives it a code.
Fifth, it uses those answers to print " (your name) has a beautiful
(part of your body)."
2. Think up your own programs using INPUT variables to make the computer
print different things. For example, think of something silly you can have
the computer tell your friends. Then write the program so different people
can use it.
65
BASIC Programming for Kids
Answers to Practice 5
66
6.
Just
Fooling
Around
In this chapter we'll be using the BASIC words you've learned so far to write
some programs just for fun. But first, let's stop and review all the things you
have learned.
67
BASICProgramming for Kids
3. How to use the comma and the semicolon to change the spacing in your
programs.
4. How to stop a program in the middle.
5. How to erase a line from a program, and how to insert new lines into a
program.
6. How to clear the screen, and how that is different from erasing the pro
gram.
If you're confused about any of these things, now is the time to stop and re
view them before you go on. (Check the index to help you find the right page.)
In the rest of this chapter, you'll get some more practice using these various
parts of BASIC to writesome programs you can try out on your friends for fun.
2. Keep It Clean.
You'll notice that the last item on the list just above mentions clearing the
screen. It's alwaysa good idea when you start a program to do it on a nice clean
screen. Instead ofhaving to type the command that clears the screen every time
you run a program, you can write a line into your program to make the com
puter erase the screen automatically. Doing this inside a program is a little dif
ferent from doing it in immediate mode. Here's how you write that line in a
program for each of our six computers:
* For the Apple, you type the line number and the word HOME, like this:
10 HOME
<*» For the Atari, you type
10 GRAPHICS 0
or you may abbreviate this to
10 GR.0
(You may also type 10 PRINT "fESCl ICTRL IICLEARl"
or 10 PRINT "lElcllSHIFTllCLEARl"
but GR.0 is much easier.)
O On the Commodore, you type the line number, PRINT, quotation mark, and
hold down the ISHIFT I key while you type the ICLR/HOMEl key, and an
other quotation mark, like this:
68
Chapter 6 • Just Fooling Around
From now on when you see a line in a program that tells you to clear the
screen, you'll have to remember the correct way to do it on your computer.
We'll write it like this:
10 (clear the screen)
But you'll know that the words "clearthe screen" won't mean anything to your
computer. You'll have to translate them into your computer's language. If you
forget, just look back to the above list, or check the quick reference list at the
end of the book.
Here's an example of a program that uses that line twice:
NEW
10 (clear the screen)
20 PRINT "WHAT'S YOUR NAME?"
30 INPUT N$
40 (clear the screen)
50 PRINT "HI ";N$;
60 GOTO 50
A On the Atari you add: 1 DIM N$(20)
This is one of those nonstop programs that you have to break into in order to
make it end.
69
BASICProgramming for Kids
3. A Spacy Remark.
In that last program did it bother you to have your name run together with hi?
If so, why don't you fix it by changing the last semicolon to a comma so line 50
looks like this:
50 PRINT "HI ";N$,
Remember, the comma makes the computerspace over to another column. But
if you're like most people, that's the kind of thing you tend to forget because
you don't use it very much. One thing you can do to help you remember what
the different parts of your program are doing is to put in remarks to yourself.
Here's how that program might look with a REMark line:
10 (clear the screen)
20 PRINT "WHAT'S YOUR NAME?"
30 INPUT N$
40 (clear the screen)
45 REM COMMA MAKES COMPUTER SPACE IN COLUMNS
50 PRINT "HI ";N$,
60 GOTO 50
The REMark in line 45 doesn't make the computer do anything. It just helps
the person writing the program remember why he or she put something in
there. When you write longer programs, sometimes you want to put in REM
lines to keep you from getting confused. REM lines make it easier for a person to
read your program listing and figure out why the program works a certain way.
4. A Crazy Letter.
Did you ever get one of those computerized letters that prints your name to
make it look personal? You know the company is sending the same letter to
thousands ofpeople, but the computer makes it seem as though the letter is just
for you. For example, Betty from Boston might get a letter like this:
DEAR BETTY,
I HAVE JUST LEARNED THAT A GIANT GREEN POLAR BEAR WILL SOON
70
Chapter 6 • Just Fooling Around
ATTACK BOSTON. IF YOU SEND ME 300 DOLLARS I WILL TELL YOU HOW
TO KEEP THIS POLAR BEAR FROM BITING OFF YOUR NOSE.
On the other hand, Danny from Dallas might get a letter like this:
DEAR DANNY,
I HAVE JUST LEARNED THAT A GIANT PURPLE SHEEPDOG WILL SOON
ATTACK DALLAS. IF YOU SEND ME 78 DOLLARS I WILL TELL YOU HOW TO
KEEP THIS SHEEPDOG FROM BITING OFF YOUR BIG TOE.
As you can see, these letters are pretty much the same except for six things:
1. The name of the person
2. The city
3. The kind of animal
4. The color
5. The number of dollars
6. The part of the body
Imagine that you had written the letter with blanks where you would fill in
those six things. (For example, the letter would start out "Dear ,I
have just learned that a giant will soon attack ")
Every blank is something that changes in each different letter. Before you can
finish each letter, you have to fill in the blanks.
To the computer, each blank would be a variable. So when you write a com
puter program to print this letter, you have to start out by filling in the
blanks — that is, by telling the computer what to put in for each variable.
We'll do this with INPUT lines, like this:
NEW
5 (clear the screen)
10 PRINT "PLEASE TYPE YOUR NAME."
20 INPUT N$
30 PRINT "WHAT TOWN DO YOU LIVE IN?"
40 INPUT T$
50 PRINT "NAME A COLOR."
60 INPUT C$
70 PRINT "NAME AN ANIMAL."
71
BASIC Programmingfor Kids
80 INPUT A$
90 PRINT "TYPE A NUMBER."
100 INPUT N
110 PRINT "NAME A PART OF YOUR BODY."
120 INPUT B$
A Remember, on the Atari add this line:
1 DIM N$(20),T$(20),C$(20),A$(20),B$(20)
Now — when we run the program, we will be filling in all those blanks. The
last part of the program will be printing the letter. Let's put in a REM line to
remind us that's what comes next.
130 REM START TO PRINT LETTER
135 (clear the screen)
140 PRINT "DEAR ";N$;","
150 PRINT "I HAVE JUST LEARNED THAT A GIANT ";C$
160 PRINT A$; " WILL SOON ATTACK " ;T$;"."
170 PRINT "IF YOU SEND ME ";N;" DOLLARS IMMEDIATELY,"
180 PRINT "I WILL TELL YOU HOW TO KEEP THIS ";A$;" FROM BITING
OFF YOUR ";B$;"."
Special notefor TRS-80: The TRS-80 computer normally gives you enough space
to store a total of 50 characters in all the string variables in your program. This
program has 5 string variables. If you type in 4 names with 10 characters,
you've used 40characters. If you then try to make your fifth variable a word (or
words) with 11 characters you'll be up to 51 and OUT OF STRING SPACE. So
you'll get an error message. If that happens, type this: CLEAR 100. That will
give you 100 letters and spaces worth of string space. You can write that line
into your program if you wish, by typing:
1 CLEAR 100.
72
Chapter 6 • Just Fooling Around
DEAR (name),
IWAS SORRY TO HEAR YOUR (body part) TURNED (color) AND FELL OFF. I
HAVE A (animal) WITH AN EXTRA (body part) THAT I WILL SEND TO YOU
JN (city) FOR ONLY (number) DOLLARS.
You can easily change that program by putting in a GOTO line that will
make it skip over the first letter. Then start your new letter at line 190. Since
the first line (Dear ) of both letters is the same, put your GOTO line
between 140 and 150, like this:
145 GOTO 190
Before you look at the answer, try to write the new letter yourself. Begin at
line 190, using the variables we already have in the program to fill in the
blanks. Study lines 150 to 180 above to get the idea. After you've tried it, check
yourself against our example. (The answer is in the Answer section for Chapter
6.)
You might want to save this program on tape or disk, so you can change it
again to write your own crazy letter, either using the same variables, or some
different ones. If you do come back to this program again, you'll probably be
glad to have the REM lines to remind you what the program is doing and where
the letter starts.
73
BASIC Programming for Kids
PETER'S LIMERICK:
A PERSON NAMED PETER FROM DREGG
ONCE ATE 29 SERVINGS OF EGG
IT WAS QUITE A DISH
AND IT TASTED DELISH
BUT IT GAVE PETER A VERY FAT LEG.
74
Chapter 6 • Just Fooling Around
You might like to try making up your own program to write a limerick or
some other type of poem.
Practice 6
1. Translate each of these English lines into a line in a BASIC program. The
finished program will print a silly story. (Boy, girl, and food are variables.)
10 The computer clears the screen.
20 The computer asks you for the name of a boy in your class. (A PRINT line.)
30 The computer waits for your answer, and gives your answer a string vari
able code name. (An INPUT line.)
40 The computer asks you for the name of a girl in your class.
50 The computer waits for your answer and gives it a code.
60 The computer asks you to name a kind of food.
70 The computer waits and codes.
80 The computer clears the screen.
90 Make a note to yourself to remind you that the story begins here.
100 The computer prints (boy) loves (girl) and (girl) loves (boy).
110 The computer prints (boy) brought (girl) some (food).
120 The computer prints (boy) loves (food) but (girl) hates (food).
130 The computer prints (girl) throws (food) at (boy).
140 The computer prints And now (boy) hates (girl) and (girl) hates (boy).
2. Look back to the program in Section 4 of Chapter 6 (A Crazy Letter). In
that program why does line 20say INPUT N$ while line 100 says INPUT N?
3. Write a program using INPUT variables to make the computer print a for
tune like this:
IN (number) YEARS YOU WILL GO BY (a kind of travel) TO (a faraway place).
YOU WILL MARRY SOMEONE WHO LOOKS LIKE (a TV star). YOU WILL
HAVE (number) CHILDREN WHO WILL ALL LOOK LIKE (a movie monster.)
Everything in the parentheses will be a variable in the program.
4. Make up your own program using INPUT variables to write a story.
75
BASIC Programming for Kids
Answers to Practice 6
Answer to the program in Section 5 (Changing the Letter): You should add
new lines beginning after 180. Depending on how you divide up your letter,
you may have more or fewer total lines in the program. The finished program
should look something like this:
5 (clear the screen)
10 PRINT "PLEASE TYPE YOUR NAME."
20 INPUT N$
30 PRINT "WHAT TOWN DO YOU LIVE IN?"
40 INPUT T$
50 PRINT "NAME A COLOR."
60 INPUT C$
70 PRINT "NAME AN ANIMAL"
80 INPUT A$
90 PRINT "TYPE A NUMBER."
100 INPUT N
110 PRINT "NAME A PART OF YOUR BODY."
120 INPUT B$
130 REM START TO PRINT LETTER
135 (clear the screen)
140 PRINT "DEAR ";N$
145 GOTO 190
150 PRINT "I HAVE JUST LEARNED THAT A GIANT ";C$
160 PRINT A$; "WILL SOON ATTACK ";T$;"."
170 PRINT "IF YOU SEND ME ";N;" DOLLARS IMMEDIATELY,"
180 PRINT "I WILL TELL YOU HOW TO KEEP THIS ";A$;" FROM BITING
OFF YOUR ";B$;"."
185 REM END OF FIRST LETTER
190 PRINT "I WAS SORRY TO HEAR YOUR ";B$
200 PRINT "TURNED ";C$;" AND FELL OFF."
210 PRINT "I HAVE A ";A$;" WITH AN EXTRA ";B$
76
Chapter 6 • Just Fooling Around
220 PRINT "THAT I WILL SEND TO YOU IN ";T$;" FOR ONLY ";N;" DOL
LARS."
A Atari: As noted earlier, do not forget the DIM line.
Note: The REM line at 185 is optional, but it will help you remember how the
program works. You may have your PRINT lines from 190 on beginning and
ending at different places in the letter. Any way you divide them up is okay —
but you can't make them too long, or your computer will complain.
Answers to Practice programs:
1. 10 (clear the screen — Use the correct line for your computer, as explained
in Section 2 of Chapter 6.)
20 PRINT "NAME A BOY IN YOUR CLASS."
30 INPUT B$
40 PRINT "NAME A GIRL IN YOUR CLASS."
50 INPUT G$
60 PRINT "NAME A FOOD."
70 INPUT F$
80 (clear the screen)
90 REM STORY BEGINS HERE
100 PRINT B$ ;" LOVES ";G$;" AND ";G$;" LOVES ";B$;"."
110 PRINT B$ ;" BROUGHT ";G$;" SOME ";F$;"."
120 PRINT B$ ;" LOVES ";F$;" BUT ";G$;" HATES ";F$;"."
130 PRINT G$ ;" THROWS ";F$;" AT ";B$;"."
140 PRINT "AND NOW ";B$;" HATES ";G$;" AND ";G$;" HATES ";B$;"."
A Atari, add: 1 DIM B$(20),G$(20),F$(20)
2. Line 20 says N$ because you are supposed to type a word, and you must use
a string variable to stand for a word. Line 40 says N because you are sup
posed to type a number, and you should use a plain letter to stand for a
number. (Actually you could use a string variable to stand for the number,
as long as you weren't planning to do any math with the number. But it's a
good idea to stay in the habit of using string variables for words and nu
meric variables for numbers.)
3. 10 (clear the screen)
77
BASIC Programmingfor Kids
78
7
Yes and No
with IF
and THEN
79
BASICProgramming for Kids
Run this program, first answering "yes" to the computer's question, and
then answering "no."
Do you understand how lines 30 and 40 work? In line 20 you told the com
puter that A$ would stand for whatever answer you (or whoever runs the pro
gram) type(s) in. In line 30 the computer checks to see if this answer (A$) is yes.
IF the answer is "yes," THEN the computer prints "I like you, too." IF the an
swer is "no" (line 40), THEN the computer comes back with "You are a snob."
IF and THEN allow your programs to make choices.
Now see what happens if you answer anything other than "yes" or "no." For
example, answer "maybe." What happens? Nothing happens, of course, be
cause you haven't told the computer what to say in that case. In fact, you could
even type an answer like "Nope," and the computer would not have any idea
that "nope" means the same as "no."
What you need to do is to add one more line that will tell the computer what
to do if a person gives any other answer besides "yes" or "no." We'll just have
the computer politely ask you to answer "yes" or "no." Then we'll make it go
back to ask the question ("Do you like me?") again.
In order to do this, you will have to have two BASIC statements in line 50,
PRINT and GOTO. Whenever you combine two statements like that in one line,
you must put a punctuation mark between them. The punctuation you use to
separate two statements is a colon, which looks like two dots, one on top of the
other(:). Here's our new line:
50 PRINT "PLEASE TYPE YES OR N0.":G0T0 10
Add that line, then run the program. You will see that the program now works
fine if you answer "Maybe" or "I don't know" or anything except yes or no. But
it doesn't work so well when you do answer yes or no.
gram to do when someone answers "yes" or "no"? You want it to stop right
there, don't you? So let's just tell the computer to end the program as soon as it
gets an answer of either "yes" or "no."
First, break into the program by hitting the key (or keys) to stop the program
when it is stalled at an input line. If you've forgotten what to press, go back and
check Chapter 4. (Or use the Quick Reference Guide, Appendix I.) Now all you
have to do is to add the word END at the end of line 30 and line 40. END simply
means "This is the end of this program." Don't forget, you'll have to put in a
colon first, because you'll be putting two statements in one line.
Here's how the finished program should look:
10 PRINT "DO YOU LIKE ME?"
20 INPUT A$
30 IF A$="YES" THEN PRINT "I LIKE YOU TOO.":END
40 IF A$="NO" THEN PRINT "YOU ARE A SNOB.":END
50 PRINT "PLEASE TYPE YES OR NO.":GOTO 10
Now the computer will know to end the program as soon as it gets a "yes" or
"no" answer. The only way it gets to line 50 is when the person does not type in
"yes" or "no."
81
BASIC Programming for Kids
Line 40 means "If you type in any number that is not 12, the computer will tell
you it fooled you."
Try that program on grownups. It seems to fool them more often than kids.
They are usually more likely to say that the answer should be 4.
Question to test your memory: Why does line 20 in this program say INPUT
A, while line 20 in the first program above said INPUT A$? If you've forgotten
the difference, better go back and review Chapters 4 and 5.
Using IF . . . THEN, you can program the computer to give a math test and
check the answers. By adding GOTO, you can have the program tell you to keep
trying till you get it right. Let's try it first with an easy problem:
NEW
10 PRINT "WHAT IS 2 + 2?"
20 INPUT A
30 IF A = 2 + 2 THEN PRINT "RIGHT!"
40 IF A <> 2 + 2 THEN PRINT "SORRY, TRY AGAIN": GOTO 10
Notice that in lines 30 and 40 you don't have to tell the computer that the an
swer is 4. Remember that the computer always knows the answer to a math
problem.
Run the program and see what happens when you get the problem wrong.
Now change this program to make the problem harder.
82
Chapter 7 . Yes and No with IF and THEN
40 INPUT B
50 PRINT "WHAT IS THE SUM OF ";A;" AND ";B
60 INPUT C
70 IF C = A + B THEN PRINT "GOOD WORK":GOTO 10
80 IF C <> A + B THEN PRINT "NO, TRY AGAIN" : GOTO 50
Run this program till you are tired of it. (When you are ready to stop it,
you'll have to break into it as you did with the program in Section 2 of this
chapter.) After you see how this program works, list it and study the listing
carefully to be sure you understand it.
Now change this program so you can use it to check you on your multiplica
tion tables instead of on addition problems.
Note: Line 80 tells you what to do if you get the problem wrong (that is, IF
C<>A+B). Actually, the only way you will get to line 80 is by typing in the
wrong answer. If you get the problem right (line 70), the program goes back to
line 10. So you can rewrite line 80 to be simply
80 PRINT "NO, TRY AGAIN": GOTO 50
Using IF and THEN with GOTO allows your programs to branch in different di
rections. Think of your program as a map. An IF . . . THEN line represents a
crossroad — a point where the computer must decide which way to go next. In
this program IF you have the correct answer, THEN the computer chooses the
road that takes you to a new problem. IF your answer is wrong, THEN the
computer returns to the old problem to give you another chance to answer cor
rectly.
6. A Variety of Choices.
You've seen how to use the equal sign (=) and the sign that means not equal to
(<>) in programs with IF and THEN. You can also use the greater than > or
less than < signs alone. For example, if you wanted to include only the num
bers with "teen" in their names, you'd want every number greater than 12 and
less than 20. That would be like line 40 in the program below.
83
BASIC Programming for Kids
You can also combine the equal sign with the greater or lesser sign. For ex
ample, this line
IFA<=10THENGOTO50
would mean "If A is 10 or any number smaller than 10, then go to line 50."
(Some computers make you put the equal sign last in that combination; others
don't care.)
Here's a program using some of these symbols in different ways:
NEW
10 PRINT "HOW OLD ARE YOU?"
20 INPUT A
30 IF A<=12 THEN PRINT "THAT'S AN AWESOME AGE!"
40 IF A>12 AND A <20 THEN PRINT "THAT'S A DYNAMITE AGE!"
50 IF A>=18 THEN PRINT "OLD ENOUGH TO VOTE!"
This program will give you one answer if you're 12 or younger, another answer
if you're between 13 and 19, and a third answer if you're 18 or over. Try adding
some lines to give it more answers for other ages.
84
Chapter 7 . Yes and No with IF and THEN
85
BASIC Programming for Kids
60 END
70 PRINT "FOOLED YOU! THERE ARE ALWAYS 12 IN A DOZEN."
80 END
And in Section 4 the program would look like this:
10 PRINT "WHAT IS 2+2?"
20 INPUT A
30 IF A=2+2 THEN 50
40 IF A<>2+2 THEN 70
50 PRINT "RIGHT!"
60 END
70 PRINT "SORRY, TRY AGAIN."
80 GOTO 10
In other words, any time you use IF. . THEN on the TI, you will have to
write the program so that each IF sends the program to a certain line number.
You must also be sure that you put in a line to tell the program when to end, or
when to go on to another section, when you have finished with the IF part.
86
Chapter 7 . Yes and No with IF and THEN
Practice 7
1. Write a program in which the computer asks someone what school he or she
goes to. If the person answers with the name of your school, the computer
87
BASIC Programming for Kids
gives one reply. If the person answers with the name of any other school, the
computer says something different.
2. Below is the beginning of a program to test your knowledge about com
puters. Read through the first five lines to be sure you understand how they
work.
10 PRINT "THIS IS A QUIZ PROGRAM ABOUT COMPUTERS."
20 PRINT "WHAT IS THE NAME OF THE LANGUAGE FOR THIS COM
PUTER?"
30 INPUT A$
40 IF A$="BASIC" THEN PRINT "RIGHT!"
50 IF A$o"BASIC" THEN PRINT "NO, TRY THAT AGAIN.":GOTO 30
Now you add at least two more questions to this program about computers.
Here are a couple of ideas, but you can use your own questions if you prefer.
(We're not going to tell you the answers though. If you aren't sure, you'd better
go back and start over again in Chapter 1.)
Sample questions to program:
1. A list of numbered instructions to the computer is called a "
2. What word tells the computer to start doing the program?
TI On the Texas Instruments computer, the program will have to look like this
from line 40 on:
40 IF A$="BASIC" THEN 70
50 PRINT "NO, TRY AGAIN"
60 GOTO 20
70 PRINT "RIGHT!"
T/S The Timex Sinclair program could be essentially the same as the TI, except
that you must add GOTO after THEN in line 70.
Answers to Practice 7
88
Chapter 7 . Yes and No with IF and THEN
89
8
Clean Up
Your
Act
In this chapter you'll learn how to get rid of some sloppy problems and make
your programs look neat and professional.
90
Chapter 8 • Clean Up Your Act
After you have all the lines for that program correctly typed in, run it. Notice
how the program prints the second question right away, as soon as you answer
91
BASIC Programming for Kids
the first question correctly. It would be nicer if the computer would give you a
chance to stop and catch your breath between questions.
You'll remember that the word to make the computer pause until the person
hits IRETURN I (or IENTERh is INPUT. So you can put in an INPUT line to stop
the program after each question. Then make the computer clear the screen, so
it prints each question on a new "page."
To do this, you could insert a short routine like this after each question:
51 PRINT
52 PRINT "PRESS RETURN TO GO ON."
53 INPUT R$
54 (clear the screen)
Note: In Line 51, the word PRINT all by itself just makes the computer skip a
line without printing anything on it. That spaces the program more attrac
tively.
TRS Line 52 on a TRS-80, TI, or Timex Sinclair should say
ti t/s 52 PRINT "PRESS ENTER TO GO ON."
Type these lines in and run the program to see how they work.
92
Chapter 8 • Clean Up Your Act
IRETURN! and clears the screen. Then it goes back up to print the next ques
tion, right where it left off.
The way to tell the computer to jump to line 2000 is this:
GOSUB 2000
GOSUB is something like GOTO in that it makes the computer skip lines in the
program. However, you might think of GOTO as a one-way ticket and GOSUB
as a round trip.
Whenever you tell the computer to GOSUB, it must know when it has fin
ished its little trip so it can jump back to the place it came from. In order to tell
the computer to go back where it started, you type the word RETURN at the
end of your subroutine. (For Apple, Atari, or Commodore computers, note that
this isnot the same as hitting the IRETURN"! key. At the end of a subroutine, you
type in the whole word RETURN.)
When you start writing longer programs, you'll often have many subroutines.
It's a good idea to put one of those REM statements at the beginning of each
subroutine to remind you what it's going to do.
So — here is the finished subroutine. Type it in. (Don't forget to use ENTER
instead of RETURN in line 2020 but not in line 2050 on the TRS-80, TI, or
Timex Sinclair):
2000 REM CLEAR SCREEN BETWEEN QUESTIONS
2010 PRINT
2020 PRINT "PRESS RETURN TO GO ON."
2030 INPUT R$
2040 (clear the screen)
2050 RETURN
5. Insert as Needed.
Now every time you want to use that routine, you just insert a line that says
GOSUB 2000. In this program you want to put it after line 10 (which is the
93
BASICProgramming for Kids
title of the program), then after line 50, after line 90, and after line 130, before
you add another question. So you will type GOSUB 2000 at lines 15, 55, 95,
and 135. As you add more questions to the program, insert the GOSUB 2000
line before you type each question. (You will also want to erase lines 51, 52, 53,
and 54 since you won't need them anymore. If you've forgotten how to erase a
line, look back to Chapter 2.)
Your program should now look more or less like this. (But if you have a
Texas Instruments or Timex Sinclair, you must rewrite the IF . . . THEN rou
tines as explained in Chapter 7. And don't forget the DIM line on the Atari.)
5 (clear the screen)
10 PRINT "THIS IS A QUIZ PROGRAM ABOUT COMPUTERS."
15 GOSUB 2000
20 PRINT "WHAT IS THE NAME OF THE LANGUAGE FOR THIS
COMPUTER?"
30 INPUT A$
40 IF A$="BASIC" THEN PRINT "RIGHT!"
50 IF A$o"BASIC" THEN PRINT "NO, TRY THAT AGAIN.":GOTO 20
55 GOSUB 2000
60 PRINT "A LIST OF NUMBERED INSTRUCTIONS TO THE COMPUTER IS
CALLED A "
70 INPUT A$
80 IF A$="PROGRAM" THEN PRINT "GOOD WORK!"
90 IF A$o"PROGRAM" THEN PRINT "NO, THINK AGAIN.":GOTO 60
95 GOSUB 2000
100 PRINT "WHAT WORD TELLS THE COMPUTER TO START DOING THE
PROGRAM?"
110 INPUT A$
120 IF A$="RUN" THEN PRINT "EXCELLENT!"
130 IF A$o"RUN" THEN PRINT "SORRY, PLEASE TRY AGAIN.":GOTO
100
135 GOSUB 2000
2000 REM CLEAR SCREEN BETWEEN QUESTIONS
94
Chapter 8 • Clean Up Your Act
2010 PRINT
2020 PRINT "PRESS RETURN TO GO ON."
2030 INPUT R$
2040 (clear the screen)
2050 RETURN
6. A Happy Ending.
When you have the GOSUB lines inserted and you run the program, you'll no
tice that there is one little bug in it. What error message do you get after you
finish the program?
That error message happened because you haven't told the computer where
you want to stop the program. List the program and you will see that right now
the last line in the program is 2050 RETURN. The computer comes to the end
of the program in the middle of a round trip. It wants to go back somewhere,
but it has run out of lines and it has nowhere to go back to.
Fortunately, a BASIC program is different from a book or a movie. It doesn't
have to end at the last line. You can tell the program to end anywhere you
want just by putting in the word END (or on the Timex Sinclair, the word
STOP). Since we want to end our program just before line 2000, we'll put in an
END line right before that. Just type:
1990 END
95
BASIC Programming for Kids
Any time you write a program that includes something you want to repeat
more than two or three times, you should put that repeating part in a subrou
tine. Here's an example of another way to write that quiz program using more
than one subroutine. Notice that you can go from one subroutine to another.
GOSUB 1000 sends the computer to check whether your answer is correct. If
it is correct, then GOSUB 2000 prints YES in fancy letters. If it's not correct,
then GOSUB 3000 prints NO in fancy letters and gives you a choice: Do you
want to try again, or do you want to see the answer? If you type "A", the pro
gram prints the answer (A$). If you type "T", you then return to the main part
of the program for another chance. GOSUB 4000 is the routine to clear the
screen before starting a new question.
The illustration on page 98 shows you how to use graph paper to plan where
to put the stars for the fancy letters. When you type in the subroutine, you may
find it easier to follow the graph paper, so you can count the spaces. Once you
get the idea, you can take a piece of graph paper and make your own designs to
print on the computer. (On the other hand, if you don't want to bother with
the fancy letters spelling YES and NO, just make up your own simpler right or
wrong messages in subroutines 2000 and 3000.)
Here's the program. If you type it in, save it. We'll be using it again in Chap
ter 10.
NEW
5 (clear the screen)
10 PRINT "THIS IS A QUIZ PROGRAM ABOUT COMPUTERS."
15 GOSUB 4000
96
Chapter 8 . Clean Up Your Act
97
BASIC Programming for Kids
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
X X X X X X X X
X X X X X
X X X X X
X X X X X
X X X X X
X X X X X
X X X X X X X X
X X X X X X X X X X X X
X X X X X
X X X X
X X X X X X X X X X
X X X
X X X X
X X X X X X X X X X X
98
Chapter 8 • Clean Up Your Act
99
BASIC Programming for Kids
TI Timex Sinclair, you must rewrite any lines that include a colon. The TI
T/S must also use a special IF . . . THEN format explained in Chapter 7. And the
Timex Sinclair uses STOP instead of END (line 995).
This program uses subroutines to save you from typing those IF . . . THEN
lines after every question. Every time you ask a new question you change the
variable A$ to equal the correct answer. The subroutine 1000-1030 checks to
see whether the answer typed in for INPUT equals the value of A$ at that mo
ment. If it does, then it goes to the YES subroutine beginning at 2000. If the
answer was wrong, it goes to the subroutine at 3000 to print NO. Next you may
choose whether to try again or see the answer. Lines 4000-4040 are the familiar
clear the screen routine.
Try adding a few of your own questions to this program to be sure you un
derstand how it works.
Practice 8
1. Add at least three more questions to the quiz program on computers that
you started in Practice 7. When you have finished, debug the program to
make sure all these problems are taken care of:
• Are all your PRINT lines typed correctly so that words don't get split in
funny places when you run the program?
• Did you remember to call up the subroutine to make the program clear the
screen between every question?
• Did you run the program with right answers and wrong answers to make
sure it works correctly both ways?
2. Write your own quiz program about a subject you have studied in school or
about something you have learned on your own. Or maybe you would like
to use tricky questions or brain teasers. (See the program QWERT in Section
1 of Chapter 12 for an example of that type.)
No answers are given to the practice problems in this chapter. The correct
answers will come from you when you have finished and debugged your pro
grams. Any program that works the way you want it to is right!
100
9.
Make Your
Computer
a Gambler
So far in your programming with variables, you have told the computer exactly
what to do. You have made X=8, or you have made N$="NANCY". But some
times — for example, when you are playing a game — you want the computer
to decide what to do.
Suppose you wanted to program a game that involved guessing a number. It
wouldn't be much fun if you already knew the number before you started.
What you want to do is to make the computer pick a number at random. A ran
dom number is like what you get when you roll dice, or pick a card, or toss a
coin. You can make the computer pretend to do any of those things with the
function RND.
1. Random Fractions.
The RND function will make the computer pick a random number between 0
and 1. Here's how it works on different computers:
On the Apple, Atari, and Commodore computers, you type
* A O PRINT RND(l)
On the TI and Timex Sinclair you type
ti t/s PRINT RND
Note: On the Timex Sinclair you get RND by typing ISHIFT I IENTER! T
101
BASICProgramming for Kids
TI t/s NEW
10 PRINT RND
20 GOTO 10
trs NEW
10 PRINT RND(0)
20 GOTO 10
and you will see the computer print a whole string of different random num
bers.
Usually, however, you don't want to have decimal fractions in a program with
random numbers. For example, if you want the computer to pretend to roll
dice, you want it to pick at random a whole number between 1 and 6. The
TRS-80 computer makes it very easy to do this, because it automatically
rounds off when you put a number 1 or larger in the parentheses after RND.
Most other microcomputers don't automatically round off, so you have to go
through a somewhat more elaborate routine to make them print a useful ran
dom number. To get a random number between 1 and 6, on the Apple, Atari,
or Commodore computers, for example, you would use this formula:
* A fr NEW
10 R = INT(RND(1) * 6) + 1
20 PRINT R
On the TI or Timex Sinclair, type:
ti t/s NEW
102
Chapter 9 • Make Your Computer a Gambler
3. Computer Dice.
The TRS-80 can use that same INT formula, although you must say RND(0)
instead of RND(l). But the TRS-80 also has a much easier way to do it. The
simplest way to make the TRS-80 computer print a random number between 1
and 6 is this:
trs NEW
10 RANDOM: R = RND(6)
20 PRINT R
(The RANDOM command makes the computer keep changing the random
numbers.)
On other computers use the function described in Section 2:
« A C= NEW
10 R=INT(RND(1)*6)+1
20 PRINT R
TI T/s NEW
10 LET R = INT (RND*6) + 1
20 PRINT R
103
BASIC Programming for Kids
Type in the program for your computer and run it a few times. Each time
you should get a different number between 1 and 6, just like rolling a die. (You
can add the line
30 GOTO 10
if you want to make the computer keep on rolling the die over and over.)
If you want to roll a pair of dice, you will need two variables. Add this line to
your program:
* A O 15 L=INT(RND(1)*6)+1
ti t/s 15 LET L=INT(RND*6)+1
trs 15 L=RND(6)
Now change line 30 to read:
30 PRINT L,R
104
Chapter 9 • Make Your Computer a Gambler
many numbers to count. (If you count on your fingers, starting with 10 and
ending with 20, you will see that you count 11 numbers.)
The rule to remember is: Take the high number (we'll call it H), subtract
the low number (L), and add 1. That number (H—L+1) is what you put after
the * sign in the RND formula. At the end insert the low number (L) after the
+ sign.
You can see how this works in the example above: (H—L+1) is the same as
(20—10+1) or 11. Here's how it looks written out in math terms as you might
use it in a program:
« A & R=INT(RND(1) * (H-L+l)) + L
ti t/s LET R=INT(RND* (H-L+l)) + L
This may seem like a very involved formula, but you don't have to memorize
it! Just remember where to look it up when you want to use it, then copy it into
your program. And don't get confused by all those parentheses. You have to
have them there to make the computer do the arithmetic correctly. Remember
that parentheses come in pairs ( ), and the above formula has three pairs.
On the TRS-80 you pick a random number between 10 and 20 like this:
trs R=RND(ll)+9
The formula is this:
R=RND(H-L+1) + L-1
You could actually put those lines in a program like this:
NEW
10 PRINT "THIS PROGRAM WILL PRINT A SERIES OF RANDOM
NUMBERS IN ANY RANGE YOU CHOOSE."
20 PRINT "TYPE THE LOW NUMBER."
30 INPUT L
40 PRINT "TYPE THE HIGH NUMBER."
50 INPUT H
* A O 60 R=INT(RND(1) * (H-L+l)) + L
105
BASIC Programming for Kids
70 PRINT R
80 GOTO 60
You can also use that formula to plug in your numbers when you write a
program for a certain purpose. For example, suppose you want to write a pro
gram in which the computer will predict how old a person will be when he or
she gets married. We'll pick 16 as the lower age limit and 60 as the higher age.
So you can write a little routine like this:
NEW
* A fr 10 A=INT(RND(1) * (60-16+1)) + 16
106
Chapter 9 • Make Your Computer a Gambler
NEW
* A o 10 C=INT(RND(1) *2) + 1
« A o 60T=INT(RND(1)*2)+1
ti t/s 60LETT=INT(RND*2)+1
107
BASIC Programming for Kids
108
Chapter 9 • Make Your Computer a Gambler
* A C 80 R=INT(RND(1) * (H-L+l)) + L
109
BASIC Programming for Kids
110
Chapter 9 • Make Your Computer a Gambler
PLEASE TYPE Y OR N.
? N
OKAY. BYE!
Practice 9
Ill
BASIC Programmingfor Kids
Answers to Practice 9
10 RANDOM: M=RND(50-2+l)+l
trs 20 W=RND(550-50+l)+49
30 C=RND(25)-1
112
Chapter 9 • Make Your Computer a Gambler
113
10.
You Can
Count on
Your Computer
Counting is one of the things a computer does best. To tell the computer to
count, you use the words FOR and NEXT, with a variable letter.
1. Count to 100.
You tell the computer to count to 100 with a program like this:
10 FOR N = 1 TO 100
20 NEXT N
That just tells the computer to count to itself. To make it "count out loud" you
have to tell it to PRINT the numbers, like this:
10 FOR N=l TO 100
15 PRINT N
20 NEXT N
RUN
Type that program and watch the numbers roll by.
Note: On the Timex Sinclair the counting will stop as soon as the screen fills up.
Press CONT to make it continue.
The letter N, you will remember, is a variable, which means it can change.
Every time the computer printed a new number from 1 to 100 it was changing
N to stand for that number.
114
Chapter 10 * You Can Count on Your Computer
2. Counting Words.
The computer can count words, too. Let's make it count to 10. But instead of
showing us the numbers 1 to 10, let's make it print HELLO 10 times. Here's the
program:
NEW
10 FOR H = 1 TO 10
20 PRINT "HELLO"
30 NEXT H
Run that program and count the hellos. Counting in BASIC can seem a little
confusing till you get the hang of it. In the next section we'll go through the
process step by step to explain how it works.
vNEXr
In the FOR line you tell the computer how many times to go around the loop. If
you say FOR A = 1 TO 1J0, the computer will go around the loop. 10 times.
Everything between FOR and NEXT will get done 10 times.
You can understand this if you pretend to be the computer, making a FOR
. . . NEXT loop. Draw a line on a piece of paper. Write FOR at one end of the
line and NEXT at the other end.
"FOR
.NEXT
Now decide how many times you want to go between FOR and NEXT. We'll
let T stand for the number of trips. If you decide to make 6 trips you write:
115
BASIC Programming for Kids
FOR T = 1 TO 6
NEXT T
Each time you make a trip from FOR to NEXT, you will do everything that
comes along the line. So let's add an activity.
/FOR T = 1 TO
CLAP
"NEXT T-
Imagine you are walking between FOR and NEXT. Every time you pass
CLAP, you must clap your hands. When you come to NEXT, you turn around
and jump back to FOR until you've used up all your trips. When you count 6
claps, you can stop, or go on to do something else.
Here's how to write that in a computer program. (Since the computer can't
clap, we'll make it print the word "clap.")
10 FOR T = 1 TO 6
20 PRINT "CLAP"
30 NEXT T
Run that program to see the computer give you 6 claps.
When we gave you those instructions, we didn't tell you to count out loud.
You should have been keepingcount silently in your head. To tell you to count
out loud in English, we might add something like this:
TOR T = 1 TO 6-
CLAP
c)SAY THE NUMBER OF CLAPS
VNEXT T-
In BASIC we would say:
10 FOR T = 1 TO 6
116
Chapter 10 • You Can Count on Your Computer
20 PRINT "CLAP"
30 PRINT T
40 NEXT T
RUN
When you tell the computer to print T, you are just telling it to count "out
loud" so we can see the numbers.
Did you ever get in trouble with your teacher and have to write something like
"I will not talk in class" 25 times? Here's a nice little program to let the com
puter do the work and save you from writer's cramp.
NEW
10 A$ = "I WILL NOT TALK IN CLASS."
20 FOR N = 1 TO 25
30 PRINT N;A$
40 NEXT N
A For Atari, add: 1 DIM A$(50)
The handy thing about that program is that you can easily change it when
you get into a different kind of trouble. Just change line 10 to:
10 A$ = "I WILL REMEMBER MY HOMEWORK."
or
5. Program a Pause.
Sometimes you will want to use FOR . . . NEXT not to make the computer print
something but just to slow it down for a few seconds. Type this program:
NEW
10 (clear the screen)
20 PRINT "WHAT'S YOUR NAME?"
117
BASIC Programming for Kids
30 INPUT N$
40 PRINT "HELLO"
50 PRINT N$
A For Atari, add: 1 DIM N$(20)
Run that program once. Then add these lines:
45 FOR A = 1 TO 600
46 NEXT A
Now run the program again. Do you see how the computer pauses in between
Hello and your name? That's because you told it to stop and count to 600 be
fore it went on. (You can see how fast the computer counts when you realize it's
going from 1 to 600 in that second or so.) Try changing the 600 to higher and
lower numbers to see longer and shorter pauses.
Each computer counts at a different rate of speed. The Apple and the Com
modore count the fastest, followed by the Atari and TRS-80. Significantly
slower is the Texas Instruments, and the real tortoise of the group is the Timex
Sinclair. In fact, you will probably want to change the 600 in line 45 to a
smaller number for the Timex Sinclair because you'll get bored waiting for it to
finish counting.
Now add this line
60 GOTO 45
to see the computer print your name with rhythm.
118
Chapter 10 • You Can Count on Your Computer
One thing to watch out for in a program with more than one FOR . . . NEXT
loop is not to get your loops tangled. Programmers say your loops have to be
nested. Here's how it works.
Imagine some instructions like this: Jump 5 times. After each jump clap 3
times. You're counting two separate things —jumps and claps — so you need
to keep track of each thing separately. If you're a computer you need two FOR
. . . NEXT loops like this:
NEW
,10 FOR J = 1 TO 5
20 PRINT "JUMP";J
,30 FOR C= 1 TO 3
40 PRINT "CLAP";C
^50 NEXT C
'60 NEXT J
RUN
Notice how the CLAP or C loop is nested inside the JUMP or J loop. Just to see
what will happen if you do it wrong, switch lines 50 and 60 like this:
50 NEXT J
60 NEXT C
When you try to run that program you'll get an error message. If you look at
the imaginary lines connecting your FOR . . . NEXT variables now, you'll see
that they're tangled up:
,10 FOR J = 1 TO 5
20 PRINT "JUMP";J
,30 FOR C = 1 TO 3
40 PRINT "CLAP";C
v50 NEXT J
v60 NEXT C
RUN
The computer runs out of its 5 jumps while it's still trying to do claps, so you
get a NEXT WITHOUT FOR error. Any time you see that error message in a
119
BASIC Programming for Kids
program, you should go back and trace your loops so you can untangle
them.
8. Stepping Up or Down.
Your computer can also count by twos, threes, fives, nines, or any other num
ber. Or it can count backward. To make it do those tricks, you add the word
STEP in the FOR . . . NEXT loop. Here's how to make the computer print the 9
times table, for example:
NEW
10 FOR X=0 TO 108 STEP 9
20 PRINT X
30 NEXT X
When you run that program, you'll see every number in the nines table from
0 times 9 to 12 times 9. STEP 9 just tells the computer to count by nines.
Here's how to make the computer count backward, like a rocket countdown:
NEW
10FORX=10TO0STEP-1
20 PRINT X
30 NEXT X
40 PRINT "BLASTOFF!!"
You can add a few lines to that program to make the computer actually act
like a rocket ship. Type these lines:
25 FOR T = 1 TO 200
26 NEXT T
50 PRINT
60 GOTO 50
Line 25 creates a pause between the numbers as it counts down. Lines 50 and
60 move the printing up and off the screen so it actually seems to take off into
space. (Don't forget to press BREAK, RUN/STOP, CTRL C, or FCTN 4 to get
yourself out of the line 50-60 loop and back in control.)
120
Chapter 10 • You Can Count on Your Computer
Here is a program using FOR . . . NEXT loops to test your reflexes. Try this with
your friends to see who can get the fastest time.
T/s Note: This program is not recommended for a Timex Sinclair because
ti the computer counts too slowly. It will work on a Texas Instruments,
but not as well as on a faster computer.
NEW
5 (clear the screen)
10 PRINT "THIS PROGRAM WILL TEST YOUR REACTION TIME."
20 PRINTiPRINT "WAIT TILL YOU SEE THE WORD 'GO'."
30 PRINT "AFTER YOU SEE 'GO' PRESS"
* A o 110 R=INT(RND(1)*6000) + 1
n 110 R=INT(RND*2000)+1
121
BASIC Programming for Kids
120 FOR X = 1 TO R
121 NEXT X
130 PRINT "GO"
140 FOR T=l TO 1000
150 PRINT T
160 NEXT T
The first several lines of that program are the instructions. If you just want to
see how the program works, start typing at line 110. Then when you type RUN,
wait for GO. As soon as you see GO, hit IBREAK I (or the equivalent for your
computer). The number you see will be your reaction time (though it's mea
sured in "computer time," not in seconds).
Here's an explanation of some of the lines:
• Line 110 picks a random number. (Since the TRS-80 and TI count slower
than the other computers, we've given them a lower range to pick from.)
• Line 120 tells the computer to count up to that random number before it
goes on. (We want a random number so you won't be able to guess how long
it's going to take before you see GO.)
• Lines 130-160: The program prints GO and starts counting. The time it takes
you between seeing GO and hitting BREAK is your reaction time.
122
Chapter 10 • You Can Count on Your Computer
123
BASIC Programming for Kids
NEW
5 (clear the screen)
10PRINT "(((((((((
20 PRINT
30 PRINT
40 PRINT
50 PRINT
60 PRINT
70 PRINT
80 PRINT
90 PRINT
100 PRINT
110 PRINT
120 FOR T=l TO 400:NEXT T
130 (clear the screen)
140 PRINT "/////////'
150 PRINT
160 PRINT
170 PRINT
180 PRINT
190 PRINT
200 PRINT
210 PRINT
220 PRINT
230 PRINT
240 PRINT
250 FOR T= TO 400:NEXT T
260 GOTO 5
124
Chapter 10 • You Can Count on Your Computer
125
BASIC Programming for Kids
NEW
10 PRINT C
20 PRINT "HI"
30 C=C+1
35 PRINT C
40 PRINT "HO"
50C=C+1
55 PRINT C
60 PRINT "HA"
70 C=C+1
75 PRINT C
80 PRINT "HEE HEE"
90C=C+1
95 PRINT C
100 PRINT "THAT MAKES ";C;" LAUGHS."
T/S On the Timex Sinclair you must start by setting C at zero, like this:
5 LET C=0
When you run that program, you should see
0
HI
1
HO
2
HA
3
HEE HEE
4
THAT MAKES 4 LAUGHS.
Do you see how the counter C = C + 1 kept track of the number of times you
printed something?
Usually you'll find a counter like that in a subroutine. Every time the com-
126
Chapter 10 • You Can Count on Your Computer
puter goes to the subroutine it adds one to keep track of the score. For example,
to write that program with a subroutine, you would do this:
10 PRINT C
20 PRINT "HI"
30 GOSUB 1000
40 PRINT "HO"
50 GOSUB 1000
60 PRINT "HA"
70 GOSUB 1000
80 PRINT "HEE HEE"
90 GOSUB 1000
100 PRINT "THAT MAKES ";C;" LAUGHS."
990 END
1000 C=C+1
1005 PRINT C
1010 RETURN
127
BASIC Programming for Kids
128
Chapter 10 • You Can Count on Your Computer
Practice 10
1. Write a program in which the computer asks you your name and then prints
it 10 times.
2. Add a timing delay loop to the program in question 1 so the computer
pauses after each time it prints your name.
3. Write a program to make the computer print the 3 times table, from 3 X 0 to
3 X 12. (Can you do this two different ways — with and without the STEP
command?)
4. Use the program in Section 8 of Chapter 8 (headed GOSUB YES or GOSUB
NO). Try adding a counter to the YES subroutine to keep track of right an
swers and one to the NO subroutine to keep track of wrong answers. Then
add 2 lines at the end of the program to print how many you got right and
how many you missed.
129
BASIC Programming for Kids
Answers to Practice 10
130
11
DATA: It's What
Your Computer
Loves to READ
DATA and READ are two words that always go together in BASIC programs.
DATA means "Here is a list of things."
READ means "Go find each item on the list, in order."
T/s Note: If you have a Timex Sinclair computer, skip this chapter. The
version of BASIC on the Timex Sinclair 1000 does not include
DATA.
1. Reading Numbers.
Type this program:
NEW
10 DATA 2,4,6
20 READ A
30 PRINT A
When you run that program, the computer prints 2. Here's why:
Line 10 means "Here is a list: 2, 4, 6"
Line 20 means "Go find the first number on the list and let A stand for
that number."
Line 30 means "Print the number that equals A."
131
BASIC Programming for Kids
The letter A in that program, as you should know by now, is a variable. So far
our programs have used variables as INPUT, as random numbers (with RND),
and for counting (as with FOR . . . NEXT). Now we're going to use variables as
pieces of DATA.
When your program said INPUT A, it meant that the person running the
program would tell the computer what A equals. When your program says
READ A, it tells the computer to go and find what A equals in a DATA line.
The first time the computer sees the instruction READ A, it will take the first
item on the list to equal A. In this program, the first item on the list was 2. So
when you told the computer to PRINT A, the computer printed 2.
2. A Second Reading.
Now list the program and add these lines
40 READ A
50 PRINT A
RUN
and the computer should print
2
4
In line 40 the computer found the instruction READ A for the second time in
the program. That made it go and find the second item on the list. Since the
second item was 4, the computer now has made A equal 4. (Remember that a
variable means something that can change. The variable A will change to a dif
ferent number every time the computer sees the instruction READ A.)
Let's tell the computer to READ and PRINT A once again. This time we'll
put both statements in one line of the program, like this:
60 READ A : PRINT A
TI Remember: If you have a TI computer, you may not use a colon. You will have to
make two lines like this:
60 READ A
65 PRINT A
132
Chapter 11 . DATA: It's What Your Computer Loves to READ
What should the computer print now when you run the program? Try it and
see if you were right.
We have three numbers in our DATA line, and we've told the computer to
READ three times. What will happen if we tell it to READ one more time? Let's
find out. Add this line:
70 READ A
Run the program, and you will see that when the computer gets to Line 70 it
is OUT OF DATA, and it will tell you so with an error message. In other words,
the computer is saying, "You told me to keep on reading, but I've run out of
things to read."
133
BASIC Programming for Kids
data. So let's tell the computer what to do when it comes to the end of the list.
Obviously, there are two things you can do when you come to the end of some
thing you are reading. Either you can stop, or you can start over again. But the
computer doesn't know which one to do unless you tell it. Let's first tell it to
stop reading when it comes to the end. Add this line:
35IFN = 6THENEND
TI But remember if you have a TI, you must follow THEN by a line number. So
you will have to add two new lines:
35 IF N = 6 THEN 50
50 END
If you are using a TI, you will also have to modify the other programs in this
chapter by putting the END command in a separate line.
Line 35 tells the computer to end the program when it comes to 6, the last
item on the list. Now run the program, and you'll see that the computer knows
when to quit.
134
Chapter 11 . DATA: It's What Your Computer Loves to READ
RESTORE means "Start over again at the beginning of the DATA." So when
you run this program now, it will just keep printing the list over and over
again. This puts the computer in an endless loop, which you will have to stop
by pressing the appropriate key. (Look back to Chapter 2 if you've forgotten
it.)
135
BASIC Programming for Kids
25 DATA END
30 READ A$
35 IF A$="END" THEN END
40 PRINT A$
60 GOTO 20
Do you understand why we had to move the IF . . . THEN line from 50 to 35?
If we left the PRINT order first, the computer would think it was supposed to
print END. So unless you have a friend whose name is END, you have to tell the
computer to stop printing as soon as it READs the word END.
With this program, you can type in your list of favorite people by changing
only one line.
How would you change line 35 to make the computer print your list over
and over again? If you've forgotten the word that means "start over," look back
in Section 5 just above.
136
Chapter 11 . DATA: It's What Your Computer Loves to READ
Notice that you must put END in the DATA line two times, so you have one
END to stand for G$ and one END to stand for B$. If you put in only one END,
the computer would run OUT OF DATA before it finished READing, because it
wouldn't be able to match up both G$ and B$ the last time it went through the
list.
You've probably figured out that we set up the DATA so that every G$ would
be a girl, and every B$ would be a boy. Then the PRINT line can just plug in
the right name from the DATA list and tell us which girl loves which boy.
If you haven't run this program yet, check it out now. You might want to
change the DATA line to put in names of your friends.
Here's a slightly different version of that program, which lets you combine
DATA with INPUT.
NEW
10 DATA MARY,BOB,SALLY,PETER,PAM,FRANK
15 DATA END.END
20 PRINT "NAME A GIRL"
30 INPUT N$
40 READ G$,B$
50 IF N$=G$ THEN PRINT G$;" LOVES ";B$
60 IF N$oG$ THEN GOTO 40
A On the Atari, add: 1 DIM G$(20),B$(20),N$(20)
Run this program, but don't worry about any OUT OF DATA error. We'll fix
that in a minute.
First, here's a description of how the program works so far:
Line 10: Gives the computer a list of girls' and boys' names.
Line 15: The end of the DATA. If you put the END statements in a separate
line, it makes it easier to change the other DATA. You can have as many
DATA lines in your program as you need to handle all your data. Just re
member to start each line with the word DATA.
137
BASIC Programming for Kids
Line 20: Asks you (or whoever is running the program) to type in a girl's name.
Line 30: Waits for you to type the name, and uses N$ to stand for the name you
type in.
Line 40: Checks the list of DATA. The first time the computer reads the list, G$
will stand for "MARY" and B$ for "BOB." The second time G$ will be
"SALLY" and B$ will be "PETER," and so forth.
Line 50: Checks to see if the girl's name you typed in (N$) is the same as the
first girl's name on the list. In other words, does N$ = G$, or in the first case,
does N$ = "MARY"? If it does, then print "MARY LOVES BOB."
Line 60: If N$ does not equal G$ (that is, if you did not type in "MARY"), then
go back to line 40. That line will make the computer check the list again to
see if it can find another G$ to match with N$.
Now so far this program will only work correctly if you type in a name on its
DATA list. Test this. Run the program and type in PAT or SUE or some other
name not on the list when the computer asks you for a name. What message do
you get?
So let's fix this program to make it work when you type a name that's not on
its list. List the program and add lines 45 and 70 so that the whole program
looks like this:
10 DATA MARY,BOB,SALLY,PETER,PAM,FRANK
15 DATA END.END
20 PRINT "NAME A GIRL."
30 INPUT N$
40 READ G$,B$
45 IF B$ = "END" THEN PRINT "SORRY, I DON'T KNOW ";N$ : GOTO
70
50 IF N$=G$ THEN PRINT G$;" LOVES ";B$
60 IF N$oG$ THEN GOTO 40
70 RESTORE : GOTO 20
Line 45 tells the computer what to do when it comes to the end of the DATA
list, so it doesn't send you an OUT OF DATA error message. Then it sends the
computer down to line 70.
Line 70 tells the computer to start the program over again. RESTORE, you
138
Chapter 11 . DATA: It's What Your Computer Loves to READ
remember, means to go back to the beginning of the DATA list, and GOTO 20
will send the computer back to give you a chance to type in another girl's
name.
Run the program (changing the DATA line if you like) and study the listing
to make sure you understand how it works.
Notice that we have not made this program end. In order to stop it, you'll
have to break into the loop at the INPUT line. Maybe you'd like to figure out
how to add some lines to give you a way out of the program.
Hint: After you RESTORE the data in line 70, don't go back to line 20. In
stead make line 70 ask you whether you want to type another name. Then
write your program to allow you that choice. You will need a PRINT statement
for the question, an INPUT line for the answer, and an IF . . . THEN statement
that starts the program over if you answer yes. If you don't answer yes, the pro
gram ends. (If you get stuck, check the solution in the answer section for this
chapter.)
9. A Loverly Quiz.
By changing part of this program, it becomes a quiz:
NEW
10 DATA MARY,BOB,SALLY,PETER,PAM,FRANK
15 DATA END.END
20 READ G$,B$
30 IF B$="END" THEN END
40 PRINT "WHO DOES ";G$;" LOVE?"
50 INPUT L$
60 IF L$=B$ THEN PRINT "YES!"
70 IF L$oB$ THEN PRINT "NO, ";G$;" LOVES ";B$
80 GOTO 20
A Atari, add: 1 DIM G$(20),B$(20),L$(20)
Type in this program and run it, then look at the line by line explanation
below:
139
BASIC Programming for Kids
140
Chapter 11 . DATA: It's What Your Computer Loves to READ
141
BASIC Programming for Kids
pose you're trying to think of a word to rhyme with "boat." First think of all
the initial consonants and blends that you can, and put them in DATA lines.
Write your program so you can put the sound you want to rhyme in an INPUT
line. Then let the computer put the words together for you. You'll come up
with a lot of nonsense words, but you'll probably also get some real words you
never thought of.
Here's the program:
NEW
10 DATA B,C,D,F,G,H,J,K,L,M,N,P,QU,R,S,T,V,W,Y,Z
20 DATA BR,BL,CH,CR,CL,FL,FR,GR,GL,PR,PH,PL
30 DATA ST,SH,SP,SCR,STR,TH,TR,THR
40 DATA END
50 PRINT "TYPE THE RHYMING SOUND"
60 INPUT S$
70 READ B$
80 IF B$="END" THEN END
100 PRINT B$;S$;" ";
110 GOTO 70
A On the Atari begin with: 1 DIM S$(10),B$(4)
Here's a sample run:
TYPE THE RHYMING SOUND
10AT
BOAT COAT DOAT FOAT GOAT HOAT JOAT KOAT LOAT MOAT NOAT POAT
QUOAT ROAT SOAT TOAT VOAT WOAT YOAT ZOAT BROAT BLOAT CHOAT
CROAT CLOAT FLOAT FROAT GROAT GLOAT PROAT PHOAT PLOAT
STOAT SHOAT SPOAT SCROAT STROAT THOAT TROAT THROAT
You can, of course, add to that program by putting more blends in the data
line. Also you can run through it again with -OTE for your ending sound and
get more rhyming words. (For example NOTE and VOTE.)
142
Chapter 11 . DATA: It's What Your Computer Loves to READ
Practice 11
1. Write a program using DATA statements to make the computer print a list
of animals.
2. Write a program using DATA statements to make the computer print a list
of animals and what they eat.
Sample run:
DOGS EAT BONES
COWS EAT GRASS
BUNNIES EAT CARROTS
etc.
1. 10 DATA DOGS,COWS,BUNNIES,CATS,ELEPHANTS,MICE
20 DATA END
30 READ A$
40 IF A$="END" THEN END
143
BASIC Programming for Kids
50 PRINT A$
60 GOTO 30
2. 10 DATA DOGS,BONES,COWS,GRASS,BUNNIES,CARROTS
20 DATA END,END
30 READ A$,B$
40 IF A$="END" THEN END
50 PRINT A$;" EAT ";B$
3. Lines 10-40 will be the same as above. Here is the rest of the program:
50 PRINT "WHAT DO ";A$;" EAT?"
60 INPUT C$
70 IF C$=B$ THEN PRINT "RIGHT":GOTO 30
80 PRINT "NO, ";A$;" EAT ";B$:GOTO 30
A. (On all programs on the Atari, don't forget to DIMension your strings.)
144
12.
Putting
It All
Together
This chapter will present just a few sample programs, showing how some peo
ple have had fun thinking up ideas for ways to use their computers. If you want
to try any of these programs, go ahead and type them in. But the main purpose
of this chapter is to give you some ideas to keep going on your own. Think
about ways you can use your computer—just for fun, to entertain your
friends, to help you with problems, or to make your life a little easier in some
way. Then either try to write your own program or look for one that's already
been written and think about whether you could make it better for you. Have
fun!
Note: These programs will work on the Apple, Atari, Commodore, or TRS-80
computers. They will have to be rewritten for the TI to avoid more than one
BASIC statement in a line. Programs with DATA statements will not work on
the Timex Sinclair 1000.
1. A Little Brain-Teasing.
This is a quiz program just for fun to prove that things aren't always the
way they seem.
145
BASIC Programming for Kids
NEW
0 REM GAME WRITTEN BY BEN AULT
5 (clear the screen)
10 PRINT "THIS GAME IS CALLED QWERT"
20 PRINT:PRINT "WHAT IS YOUR NAME?"
30 INPUT A$
40 PRINTiPRINT "OH TOUGH LUCK, ";A$
45 PRINT
50 PRINT "I WILL NOW ASK YOU SOME QUESTIONS."
55 GOSUB 1000
60 PRINT "IF A ROOSTER SITS ON TOP OF A BARN WITH A SLANTING
ROOF"
70 PRINT "WILL THE EGG ROLL OFF THE ROOF?"
80 INPUT S$
90 IF S$="YES" THEN PRINT "WRONG!"
100 IF S$="NO" THEN PRINT "CORRECT!"
110 PRINT "ROOSTERS DON'T LAY EGGS."
120 GOSUB 1000
130 PRINT "IF YOU TAKE 2 APPLES FROM 3 APPLES, HOW MANY APPLES
DO YOU HAVE?"
140 PRINT "TYPE THE NUMBER."
150 INPUT E
160 IF E=2 THEN PRINT "CORRECT":GOTO 180
170 PRINT "WRONG"
180 PRINT "IF YOU TAKE 2 APPLES YOU HAVE 2 APPLES."
190 GOSUB 1000
200 PRINT "IS IT POSSIBLE FOR JANE TO STAND BEHIND LEE"
210 PRINT "AND LEE TO STAND BEHIND JANE AT THE SAME TIME?"
220 INPUT W$
230 IF W$="YES" THEN PRINT "ABSOLUTELY!"
240 IF W$="NO" THEN PRINT "INCORRECT"
250 PRINT "THEY CAN STAND BACK TO BACK."
146
Chapter 12 • Putting It All Together
147
BASIC Programming for Kids
148
Chapter 12 . Putting It All Together
FOOLED YOU!
THERE ARE ALWAYS 12 IN A DOZEN.
2. Spelling Practice.
Here's a program to help you practice your spelling. Use line 10 (and as many
other lines as you need from 11 to 19) to type in your spelling words as DATA.
When you run the program, it flashes the words quickly on the screen and
asks you to type in the correct spelling. When you misspell a word, the program
goes to line 140, where it keeps count of the mistakes (with W) and begins a
routine that makes it print the correct spelling 9 times so you can study it. Then
it goes back to give you another chance.
Some teachers have used this program in their classrooms, with this addition:
A game (copied from a computer book or magazine, or made up by students) is
149
BASIC Programming for Kids
added to the end of the program. If the student gets all the words right (IF
W=0), he or she gets to play the game. If not, the program ends.
NEW
0 REM THANKS TO S. NOCELLA AND C. KWARCINSKI
5 (clear the screen)
10 DATA CAT,DOG,BOY,GIRL
20 DATA END
30 PRINT "SPELLING QUIZ"
40 PRINT:PRINT "PLEASE PRESS RETURN AND WATCH CAREFULLY FOR
THE WORD."
50 INPUT R$
60 (clear the screen)
70 READ W$
75 IF W$="END" THEN PRINT "THAT'S ALL THE WORDS FOR
TODAY":GOTO 250
80 PRINT W$
85 REM ADJUST NUMBER IN LINE 90 FOR LONGER OR SHORTER FLASH
90 FOR T=l TO 200:NEXT T
100 (clear the screen)
110 PRINT "TYPE THE WORD AND PRESS RETURN"
120 INPUT A$
130 IF A$=W$ THEN PRINT "GOOD SPELLING!":PRINT:PRINT "PLEASE
PRESS RETURN FOR THE NEXT WORD.":GOTO 50
135 REM LINES 140-230 ALLOW STUDENT TO REVIEW SPELLING OF
MISSED WORDS
140W=W+1
145 PRINT:PRINT "NO, WATCH CAREFULLY."
146 PRINT
150 FOR X=l TO 3
160 FOR T=l TO 100:NEXT T
170 FOR Y=l TO 3: PRINT W$;" ";
175 FOR T=l TO 100:NEXT T
150
Chapter 12 • Putting It All Together
180 NEXT Y
185 PRINT
190 NEXT X
195 FOR Z = 1 TO 2000: NEXT Z
200 PRINT:PRINT "PLEASE PRESS RETURN AND TRY AGAIN."
210 INPUT R$
220 (clear the screen)
230 GOTO 80
250 IF W=0 THEN PRINT "YOU GOT 100% RIGHT!":END
260 PRINT "YOU MISSED M;W
A On the Atari, add 1 DIM A$(20),W$(20)
Here's a sample run of the program:
SPELLING QUIZ
PLEASE PRESS RETURN AND WATCH CAREFULLY FOR THE WORD.
CAT
{Note: The word flashes on, then disappears.)
TYPE THE WORD AND PRESS RETURN
? KAT
NO, WATCH CAREFULLY.
CAT CAT CAT
CAT CAT CAT
CAT CAT CAT
PLEASE PRESS RETURN AND TRY AGAIN.
CAT
TYPE THE WORD AND PRESS RETURN
? CAT
GOOD SPELLING!
PLEASE PRESS RETURN FOR THE NEXT WORD.
DOG
TYPE THE WORD AND PRESS RETURN
? DOG
GOOD SPELLING!
151
BASIC Programming for Kids
3. Math Dice.
Here's a game that could help you practice math facts. This version uses addi
tion, but it could easily be changed to subtraction or multiplication. In this
version, we've pretended to be using regular dice, so the numbers only go up to
6. If you want to use special dice and make your numbers bigger, change the
range of random numbers.
This is a game for two players. Each one takes turns rolling two imaginary
dice. (Actually the computer rolls the dice and prints the numbers.) If you can
add the total on the two dice correctly, that total gets added to your score. You
get two tries to answer correctly — and for each incorrect answer 5 points are
deducted from your score. The computer keeps score, of course.
Here's the program:
NEW
0 REM THANKS TO P. KEEFE AND E. WARWICK
10 (clear the screen)
20 PRINT "THIS IS A GAME FOR TWO PLAYERS."
30 PRINT "YOU WILL TAKE TURNS ROLLING DICE."
152
Chapter 12 • Putting It All Together
31 PRINT: PRINT "IF YOU CAN ADD THE TOTAL OF THE TWO DICE
CORRECTLY,"
32 PRINT "THE COMPUTER WILL ADD THAT TOTAL TO YOUR SCORE."
33 PRINT:PRINT "YOU GET TWO TRIES TO ANSWER CORRECTLY."
34 PRINT "EACH WRONG ANSWER COSTS YOU 5 POINTS."
35 PRINT:PRINT "PLEASE PRESS RETURN TO START."
36 INPUT K$
37 (clear the screen)
40 PRINT "PLEASE TYPE THE NAME OF THE FIRST PLAYER."
50 INPUT F$
60 PRINT "THE SECOND PLAYER?"
70 INPUT S$
74 REM X AND Y KEEP TRACK OF WRONG ANSWERS—RESET TO 0
BEFORE EACH TURN
75 X=0:Y=0
76 (clear the screen)
80 PRINT F$;" IS ROLLING THE DICE."
90R=INT(RND(1)*6)+1
100 L=INT(RND(1)*6)+1
TRS
90 RANDOM:R=RND(6)
100 L=RND(6)
153
BASIC Programming for Kids
190 PRINT
200 PRINT S$;"'S TURN"
210 l=INT(RND(l)*6)+l
220 E=INT(RND(1)*6)+1
f 210 RAND0M:I=RND(6)
I 220 E=RND(6)
154
Chapter 12 . Putting It All Together
? 10
RIGHT! YOUR SCORE IS 10
MINNIE'S TURN:
ONE DIE SAYS 1
THE OTHER SAYS 5
WHAT IS THE TOTAL?
?6
RIGHT!
MINNIE'S SCORE IS 6
MICKEY'S SCORE IS 10
DO YOU WANT TO ROLL AGAIN?
TYPE Y FOR YES AND N FOR NO. Y
MICKEY IS ROLLING THE DICE.
THE RIGHT ONE SAYS 6
THE LEFT ONE SAYS 6
WHAT IS THE TOTAL?
? 11
NO, TRY AGAIN.
? 12
RIGHT! YOUR SCORE IS 17
MINNIE'S TURN:
ONE DIE SAYS 4
THE OTHER SAYS 2
WHAT IS THE TOTAL?
16
RIGHT!
MINNIE'S SCORE IS 12
MICKEY'S SCORE IS 17
. . . and so on.
4. Politics.
In this program you must decide how you would deal with the problems that
could arise if you were the mayor of a town. You start with $100,000. Your ob-
155
BASIC Programming for Kids
ject is to spend your money in ways that will please the voters and get you re
elected, while staying within your budget. Here's an explanation of some parts
of the program:
Lines 15-40 print the title and instructions.
Lines 45-320 involve many variables and random number routines to create
the course of events in the game. Here's a description of some of the variables:
B — the amount of money in your budget.
T — how many times you've had a problem come up.
X— picks a random number for selecting a problem from the DATA list. (It
is possible, as in real life, for the same problem to come up more than
once in the game.)
P$ — the statement of the problem.
C — the cost of solving the problem.
A — adds or subtracts a random amount to or from the estimated cost, in
order to establish the actual cost.
N — how many times you said no.
E — a random increase in the budget.
YV — your votes in the election. (The random formula for arriving at your
votes subtracts something according to how many times you voted no
on issues.)
DV — the dogcatcher's votes.
Lines 325-660 are some possible outcomes for the game — based on a combi
nation of random factors and how you decided to spend your money.
Lines 700-750 are 22 possible problems, and their estimated cost. You could,
of course, add to or change these. If you add more problems, you will have to
change the range in line 60, which is set for 22 problems. For example, if you
add five more problems, change that number to 27.
Note: If you have a TRS-80, look for the starred (***) lines and substitute the
lines at the end of the program.
NEW
10 (clear the screen)
15 PRINT ">P0LITICS"
156
Chapter 12 • Putting It All Together
157
BASIC Programming for Kids
158
Chapter 12 . Putting It All Together
Hid DATA THE FIRE HOUSE JUST BURNED DOWN! WE NEED A NEW
ONE.,30000
715 DATA A TRUCK CRASHED INTO TOWN HALL! REPAIRS ARE
NECESSARY.,5000
720 DATA THE TOWN COMPUTER BROKE DOWN—WE NEED TO HIRE
A REPAIRMAN., 1000
725 DATA THE VOTERS WANT A STATUE PUT UP TO HONOR THE
TOWN DOGCATCHER., 15000
730 DATA THE VOTERS DEMAND THAT YOU BUILD A TOWN
PARK.,50000
735 DATA THE TOWN DOGCATCHER NEEDS A NEW TRUCK AND
MORE NETS., 10000
740 DATA THE LIBRARY NEEDS TO REPLACE STOLEN BOOKS.,8000
745 DATA THE POLICE FORCE WANTS A NEW FLEET OF
CRUISERS.,45000
750 DATA THE VOTERS DEMAND THAT YOU REPAIR THE POTHOLE-
RIDDEN STREETS., 15000
755 DATA THE DOGCATCHER GOES ON STRIKE TO DEMAND MORE
PAY.,5000
760 DATA THE VOTERS WANT THE TREES TRIMMED ON THE PUBLIC
STREETS.,7000
765 DATA THE POLICE FORCE WANTS TO TRAIN NEW OFFICERS IN
WATER BALLET., 10000
770 DATA THE FIRE DEPARTMENT WANTS TO REPAINT THE HOOK
AND LADDER TRUCKS., 1500
775 DATA THE TOWN DUMP WANTS TO BUY A NEW TRASH COM
PACTOR UNIT., 20000
780 DATA THE ROADS IN FRONT OF TOWN HALL NEED
REPAVING.,10000
785 DATA THE STREET LAMPS ALL NEED NEW BULBS.,9000
790 DATA THE TOWN WANTS NEW STREET SIGNS PUT UP AT EVERY
CORNER.,15000
795 DATA TOWN OVERRUN WITH PESKY PETUNIAS! VOTERS
159
BASIC Programming for Kids
BUDGET: $100000
THE TOWN DUMP WANTS TO BUY A NEW TRASH COMPACTOR UNIT.
ESTIMATED COST: $20000
DO YOU WANT TO SPEND THE MONEY (Y/N)? N
HIT RETURN. . . ?
160
Chapter 12 • Putting It All Together
BUDGET: $100000
THE VOTERS DEMAND THAT YOU BUILD A TOWN PARK.
ESTIMATED COST: $50000
DO YOU WANT TO SPEND THE MONEY (Y/N)? Y
ACTUAL COST: $52453
VOTERS ELECT TO INCREASE YOUR BUDGET!!
HIT RETURN. . . ?
BUDGET: $48653
THE POLICE FORCE WANTS TO TRAIN NEW OFFICERS IN WATER BALLET.
ESTIMATED COST: $10000
DO YOU WANT TO SPEND THE MONEY (Y/N)? N
BUDGET: $48653
THE VOTERS WANT A STATUE PUT UP TO HONOR THE TOWN
DOGCATCHER.
ESTIMATED COST: $15000
DO YOU WANT TO SPEND THE MONEY (Y/N)? N
BUDGET: $48653
THE FIRE DEPARTMENT WANTS TO REPAINT THE HOOK AND LADDER
TRUCKS.
ESTIMATED COST: $1500
DO YOU WANT TO SPEND THE MONEY (Y/N)? N
BUDGET: $48653 . . .
ELECTIONS!!!
IT'S YOU AGAINST THE OLD DOGCATCHER.
VOTES:
FOR YOU: 855
FOR DOGCATCHER: 1145
WELL, IT LOOKS LIKE YOU LOST. BETTER LUCK NEXT TIME.
YOU HAD $33234 LEFT.
PLAY AGAIN (Y/N)? N
THANKS FOR PLAYING.
161
Appendix I: Quick Reference Guide
Here's a list of some of the features that commonly vary in different types of
home computers. If you forget how to clear the screen, for instance (or if you're
trying to translate a program from one computer to another), check this guide.
Apple: Use the left arrow key to back up the cursor. Make a correction when
the cursor is positioned on the mistake. Use the space bar to erase. Use the
right arrow key to move the cursor right without erasing.
Atari: Use the IDELETE/BACKS! key
Commodore: Use the IINST/DELl key
TRS-80: Use the left arrow key E3
Texas Instruments: Hold the IFCTN I key and press S to back up the cursor to the
position where you can correct the mistake. Use the space bar to erase.
IFCTN I D moves the cursor right.
Timex Sinclair: Use ISHIFT I 0
162
Appendix I • Quick Reference Guide
163
BASIC Programming for Kids
164
Appendix II: Troubleshooting
When your program doesn't work the way you want it to, the first step is to
type LIST. If the computer tells you SYNTAX ERROR IN 10, then type LIST 10.
Note: If your TRS-80 Model I or III tells you SYNTAX ERROR IN 10 it will also
put you into Edit mode. For more about this, see Appendix III.
If you don't know exactly where the error is, then list a few lines at a time,
like this:
* O trs ti LIST 0-50
or
A LIST 0,50
to list every line from 0 to 50.
Here are some things to check for:
• Did you misspell a keyword?
Wrong: 10 PRIMT 2+2
Right: 10 PRINT 2+2
• Did you type a 0 for a 0, or vice versa?
Wrong: 10 G0T0 20
Right: 10 GOTO 20
• Did you put your quotes in the wrong place?
Wrong: 10 PRINT "HELLO N$"
Right: 10 PRINT "HELLO ";N$
• Did you forget to type IRETURN! (or IENTER I) before starting a new line in
the program? This is a very common mistake, especially when the cursor has
automatically jumped to the beginning of a line on the screen. It's sometimes
hard to see this mistake because when you LIST the whole program it looks
okay. But when you tell the computer to LIST 10, for instance, it prints:
10 PRINT "HELLO, HOW ARE YOU?"
20 GOTO 10
In that case the computer doesn't know that 20 is supposed to be a new line; it
thinks line 20 is part of line 10 and doesn't know what to do with it. You must
remember that just putting the cursor on the left side of the screen doesn't start
a new program line. You must press IRETURN] (or IENTER [) to tell the com
puter "That's the end of line 10 — now start line 20."
165
BASIC Programming for Kids
To correct this mistake, retype both lines, and remember to press IRETURN!
at the end of line 10 (and at the end of line 20).
• Did you forget to type NEW before you started your new program? If so,
you'll see some lines from your old program mixed up with your new program
confusing things. To fix this you'll have to erase all the old lines (by typing each
line number and pressing IRETURN!).
Note: On the Apple and TRS-80 you can delete whole blocks of a program.
* On the Apple type DEL 10,50.
trs On the TRS-80 type DELETE 10-50.
That will erase every line starting with line 10 and ending with line
50.
• Did you use the wrong number of quotes or the wrong number of parenthe
ses? Remember, they come in pairs. If you count the quotation marks or paren
theses in one line and come out with an odd number, something's wrong.
Wrong: 10 PRINT "THE ANSWER IS ";N;APPLES"
Right: 10 PRINT "THE ANSWER IS ";N;" APPLES."
Wrong: 10 R=INT(RND(1) * (H-L+l) + L
Right: 10 R=INT(RND(1) * (H-L+l)) + L
• Did you put a space in the wrong place, or leave out a space where there
should be one?
Wrong: 10 DATA CAT, DOG, HORSE, COW
Right: 10 DATA CAT,D0G,H0RSE,C0W
Wrong: 10 IF A$=" YES" THEN GOTO 50
Right: 10 IF A$="YES" THEN GOTO 50
Some computers won't let you put a space in the word GOTO. Others don't
care if you type GO TO.
Some computers won't let you type expressions like RND (1) — with a space
before the (1) — instead of RND(l).
• Did you forget to put a colon between two commands in one line?
Wrong: 10 PRINT "HI" PRINT "HO"
Right: 10 PRINT "HI":PRINT "HO"
166
Appendix II • Troubleshooting
167
Appendix III: Editing Your Mistakes
On almost any program you type, you will make mistakes. The least compli
cated way to fix your mistakes is to press IRETURN! or IENTER I, type the whole
line over again, beginning with the line number, and press IRETURN] (or
IENTER Dagain. That works on every computer. It will erase the line that had
the mistake and put the new line in its place.
Sometimes, though, especially if your line is long, you really don't want to
have to retype the whole thing. So here's a quick rundown on how to use the
editing functions on your computer to let you correct mistakes without retyp
ing whole lines.
168
Appendix III • Editing Your Mistakes
5. Then press IRETURN! to enter your correction in the memory of the com
puter.
Note: Once you press IESC Iyou are in Edit mode. You may continue pressing I,
J, Kf or M to move the cursor. Pressing any other key (or even pressing IESC Ia
second time) turns I, J, K, and M back to ordinary typing keys.
The right arrow key and IESC I K both move the cursor to the right. The dif
ference is this: When the right arrow key sends the cursor over a letter, it is re-
copying the letter into the computer's memory. When you use IESC I K to move
the cursor over a letter, it tells the computer to ignore that letter.
You may use the Edit mode on a line you've just finished typing, or after you
tell the computer to LIST the line. However, when the computer lists the line, it
may print it on the screen with spaces that aren't actually part of the line. Ex
ample:
You type:
10 PRINT "HELLO, MY NAME IS APPLE COMPY
UTER." I RETURN |
LIST I RETURN I
The computer prints:
10 PRINT "HELLO, MY NAME IS APPL
E COMPYUTER."
The listing shows 12 extra spaces between the L and E in APPLE. This
makes it more complicated to use the Edit function. If you use the right arrow
key to move the cursor over those 12 spaces, you are actually putting extra
spaces into your program. So you will have to switch from the right arrow key
to IESC I K in order to move the cursor over those 12 spaces.
One way around this is to type POKE 33,33 IRETURN Ibefore you type LIST.
The line(s) will then appear without those extra spaces. When you finish your
editing (after you press IRETURN"!), then typeTEXT IRETURN] before you go on
to do anything else.
169
BASIC Programming for Kids
170
Appendix HI • Editing Your Mistakes
Place the cursor over the N. Hold the ISHIFT I key and press the IINST/DELl
key. This will open up a space before the N, with the cursor now on top of the
space. Type the missing I in the space and press IRETURN].
Notice that when using the IINST/PETI key to make corrections, you should
position the cursor just after the mistake. When you want to change one letter
into another, put the cursor directly over the mistake, and type the new letter.
And don't forget to press IRETURN] after you've made your correction.
IRETURN 1 is what tells the computer to remember the correction. You
can correct more than one mistake in a single program line before you press
IRETURN I. (For example, if you have two mistakes in line 10, correct one,
move the cursor, correct the second, then press IRETURNT). But you must press
IRETURN! before going on to edit a new line.
Possible problem: If you type a quote mark like this " and then try to use the cur
sor arrow or |INST/DEL~| keys, you will start getting funny little reversed char
acters on the screen. That's because the computer thinks you are telling it to
print the cursor arrow key instead of moving the cursor immediately.
If you need to type a quotation mark, then continue making corrections,
press IRETURN I after you type the quotation. Then move the cursor back up
again to make the rest of your corrections.
Example:
Mistake: PRINT HEELLO"
1. Move the cursor to the space after the T.
2. Type the quotation mark.
3. Press IRETURNl.
4. Move the cursor back up to the second E.
5. Press IINST/DELl to erase the extra E.
6. Press IRETURNl.
171
BASIC Programming for Kids
Here's how you can fix it, using the Edit mode on your computer.
1. First, type EDIT 10 and press IENTER I. Line 10 should reappear with the
cursor on the first character after the number 10, like this:
10 [P]RUNNT "HELO"XXX
2. Hold the IFCTN I key and type D to move the cursor over to the U. Type I to
change the U into an I.
3. To wipe out a letter, use IFCTN I 1. In this line you want to delete the extra
N, so move the cursor right again until it is on top of the N. Then type
I FCTN I 1 to erase the N.
4. To insert a letter, type IFCTN I 2. You want to insert an extra L in "HELO",
so move the cursor over to the L. Then type IFCTN I 2, and type L.
Note: Once you type IFCTN I 2, the computer will keep on inserting until you
press IFCTN I again with another key.
5. To erase the extra X's, move the cursor right with IFCTN I D. Then erase with
the space bar.
6. Press IENTER Iwhen you have finished making corrections, to enter the new
line into the computer's memory.
10 PRUNTT "HELC'XXXXXXX
In that line, you want to: 1) change the U to I; 2) delete the extra T; 3) insert
L in HELO; and 4) get rid of all those X's at the end. Here's how to do each of
those things. (If you want to practice using the Edit mode, type that line full of
mistakes, press IENTER L then follow these directions):
1. First type EDIT 10 and press IENTER |. The number 10 should appear, fol
lowed by the cursor.
2. Type L (for List). This makes the whole line, mistakes and all, reappear.
Underneath it you should again see the line number 10 and the cursor.
3. When you are in Edit mode, you use the space bar to move the cursor to
the right and use the left arrow key to move the cursor to the left. Each
time you press the space bar, one more character in your messed-up line
will appear. If you accidentally space past the mistake, use the left arrow
key to back up.
Press the space bar until you see this:
10 PR CRSR
The cursor is now under the first mistake (the U), but you can't see the U.
4. To change the last character, press C (for Change), then the new letter, I.
Presto — the U becomes an I and appears in the correct space!
5. Move the cursor by pressing the spacebar until the first T appears. In order
to get rid of the next character — the extra T — type D (for Delete), and the
T is erased. The T appears inside exclamation points like this:
10 PRINT1T! CRSR
The exclamation points are just there to show you that you've erased the T.
They won't stay there. Anything you see inside those exclamation points will
disappear when you list the line again.
6. Space the cursor over to the L. Press Ifor Insert). Type L, and you now have
HELLO spelled correctly.
7. Unfortunately, the computer has no way of knowing that you were only
going to insert one L. If you type anything else now, it will keep on insert-
173
BASIC Programming for Kids
ing. So, for example, if you press the space bar, hoping to see the 0, the
computer will insert a space in the word: "HELL 0".
In order to get out of insert mode, you must press ISHIFT I \T\.
8. Now the only thing left to do is to knock off all those extra X's. So move the
cursor over to just after the last quote mark.
9. Then type H (for Hack), and the rest of the line is erased.
10. To record your changes, press IENTER I. That takes you out of Edit mode.
11. If you want to be sure everything was corrected, type LIST 10. You should
then see the line in its final correct form:
10 PRINT "HELLO"
A few other commands you might find useful in Edit mode are these:
Q (for Quit) is what you type ifyou've made some corrections, then change your
mind about all of them. Q takes you out of Edit mode and leaves your program
line as it was before you started making changes.
Typing SB will search for the letter B, and move the cursor automatically to
the first B in the line. 2SB will search for the second B, 3SB, for the third B, etc.
(This may also be done with any other character besides B, of course.)
174
Appendix IV: What Is a Computer?
We've put this section near the end of the book because it's a lot easier to
understand how a computer works after you've played around with one
for a while. Also, you really don't need to know about what goes on inside
the computer to be able to use it. You can write perfectly good programs
without knowing anything about bits and bytes or ROM and RAM. But
most people who start fooling around with computers eventually want to
learn something more about what makes them compute. So if you're curious
— read on.
The word used to describe the machinery that makes up your computer and
the other parts that connect to it is "hardware." In order to use a computer you
need three main pieces of hardware: (1) An input device; (2) The central pro
cessing unit; (CPU) (3) An output device.
The input device lets you put information into the computer. When you
write a program, the input device you use is the keyboard. Any time you press a
key you are putting one piece of information Into the computer. Other exam
ples of input devices your computer may or may not have are: tape re
corder/player with tapes, disk drive with disks, joystick or paddles, light pen,
and touch-sensitive screen.
People are now trying to perfect computers that will accept voice input, so
that you can put information in just by saying it.
All of these devices let you send some kind of signal into the computer. The
place where they send the input is the CPU, the central processing unit. This is
the "brains" of the computer, the part that makes it work. (You probably can't
see the CPU on your computer, because it's inside the case, as your brain is in
side your head. On most personal computers the CPU and the keyboard are
parts of the same package.)
Think about your own body. You can get input through your eyes, ears,
nose, mouth, or skin. But you don't know what to do with that input until your
brain tells you. If you didn't have a brain, you wouldn't know that a fire was
hot. You would just leave your hand in a flame until it was burned to a crisp. It
175
BASIC Programming for Kids
is your brain that figures out FIRE means HOT means HURT means GET
AWAY.
The CPU inside the computer is not made of brain cells. It's made mostly of
silicon — the main ingredient in common sand (more about that later). And it's
not nearly as smart as your brain. But it has much the same job — to figure out
what to do with the information that gets sent to it as input and then to trans
late that information into useful output.
The output device used by most home computers is the TV screen. Actually, it
doesn't have to be a TV set, and some more accurate computer names for this
device are: video display terminal (VDT), cathode ray tube (CRT), or monitor.
Other types of output devices are printers (to put your output on paper) and
speakers (to make sound output). Output can also be sent to various types of
machines, which are usually hooked up to special-purpose computers such as
those in factories or laboratories. Robots in factories, for example, are output
devices whose movements are controlled by computers.
You can also make your computer a two-way communicator — sending out
put and receiving input to and from faraway places — with a device called a
modem. A modem is a connection that allows your computer to use the tele
phone lines to "talk" to other computers. If you and your friend both have
modems on your computers, you can transmit your programs back and forth
over the phone lines.
Probably the most exciting use of the modem allows your computer to tap
into the resources of much larger collections of information on other com
puters. Through this communication hookup, your computer can locate for
you facts on the latest medical discoveries, political news, or sports scores. It
can allow you to browse through catalogs, order, and pay for items without
ever leaving your home or taking out your wallet.
Your computer's input can come from a data base thousands of miles away
or from its own keyboard. No matter where the input starts, it's the output, ob
viously, that makes the computer useful to people. You must be able to see,
hear, or feel the messages the computer is sending if they're going to do you any
176
Appendix IV • What Is a Computer?
good. Your brain can tell you a fire is hot, but knowing that fact won't help you
if your brain doesn't also get the message to your muscles to take your hand
away. That final effect — the way the computer responds after processing the
input — is the output.
So how does the computer manage to process all these input messages into
usable output? It does this by breaking down complicated messages into
simpler ones so that it can rearrange them. First it painstakingly translates each
item of input into a pattern of electrical impulses — using a code made up of
patterns of l's and O's. These l's and O's represent whether individual tiny elec
trical switches inside the computer are on (1) or off (0). Early computers (back
around the 1950s) used a system of vacuum tubes to transmit on/off informa
tion. Sometimes filling whole rooms, those first computers were much larger
machines than today's microcomputers, although they were often much less
powerful in terms of what they could do. As time went on, scientists developed
ways to get the same (or a better) effect with much smaller electronic devices.
And now thousands of "on/off switches" can be packed onto a tiny square or
"chip" of silicon in what is called an integrated circuit. You can't tell by looking
at a silicon chip how it works, because its information comes in the form of in
visible electrical or magnetic signals, and because the whole thing is so tiny.
Though you can't see what the chips are doing, you can — if you have a
computer like an Apple, which makes it easy for you to look inside the cover —
open up the case and see how the chips are installed. The actual chips are
inside all those little black boxes, which are connected together by being
plugged into the circuit boards. The circuit boards are the large flat green rec
tangles with silvery tracings. These transmit the electrical signals through the
computer.
If you could see what was going on inside a chip, you would see something
like thousands and thousands of tiny switches being turned on and off. The
switches work much the same way as an electric light switch. When you push
up on a wall switch, it lets in the electric current, and the light comes on. When
you push the switch down, the current is stopped and the light goes off. The
switches inside a computer, of course, do not look like electric light switches.
177
BASIC Programming for Kids
But they do turn "on" or "off' to let electricity in or keep it out, and those
on/off patterns make up the language of the computer.
In the language of mathematics, we say that computers use binary code. That
means that every message you send the computer is eventually translated into a
0 (representing off) or a 1 (representing on). Each 0 and 1 is called a Binary
digiT,-which is abbreviated to the word bit. Bits are combined into groups of 8,
and each eight-bit group is called a byte.
Here's an example of how bits combine into bytes to stand for our letters and
numbers. (You'll notice that only 7 bits are actually used in the code. The
eighth bit is not a part of the code but is used to represent other information
that tells the computer how to carry out its operations.)
When you press an A on the keyboard, that keystroke is translated into the
series of 1's and O's that you see above. Those 1's and O's cause the tiny switches
inside the CPU to go on and off in a pattern that ultimately transmits a signal
to the video display. On the monitor that signal is transformed to a shape we
recognize as the letter A. (These patterns of O's and 1's to represent letters and
numbers are known as the ASCII code — or American Standard Code for In
formation Interchange. This code is used to form the machine language, or the
lowest level language on the computer. Inside your computer a high-level lan
guage such as BASIC gets translated into this low-level machine language.)
178
Appendix IV • What Is a Computer?
With such a simple idea behind it all, a computer can do so many seemingly
complicated things for two reasons:
1. It has a very large number of switches that can produce a great many differ
ent patterns.
2. It can perform its switching operations incredibly fast. The most powerful
computers can do millions of calculations per second. In a matter of minutes
computers can figure problems that would take a person more than a life
time to do with a pencil and paper.
But when it seems that a computer is performing miracles, remember that its
only real miracles are its size (a lot of switches compressed into a very small
space) and its speed. At the bottom of it all, everything is still reduced to those
l's and O's.
And when you think about how that works, you may understand why you
have to spell everything out for the computer in such painstakingly simple
terms. (IF A=B THEN PRINT "YES.") Everything you want a computer to do
must be translated into something that can be answered either yes or no.
That's why a computer is good at solving problems that have only one right
answer ("What is 2 + 2?"). Trouble often begins when people start expecting
the computer to give them answers to questions like "What should we do to
solve the economic problems of our government?" There are so many possible
answers to that question that probably no two computer programs would give
you the same solution.
A few more terms you should know if you want to consider yourself "com
puter literate" are ROM, RAM, and K (for Kilobyte). These all have to do with
the memory of your computer.
ROM stands for Read Only Memory. This is the memory that stays inside
your computer when you turn it off. It is permanently programmed into the
computer by the people who built it. ROM is made up of the aspects that you
always want to work the same way when you use the computer. For example,
you don't want to have to learn a different system for loading a program every
time you turn on your computer. So the information that tells the computer
"This is how you take a program from the disk and get it running" is a part of
179
BASIC Programming for Kids
180
Appendix IV * What Is a Computer:
181
Index
alphabetical order, 84 CRT (cathode ray tube), 176
animation, 122-124 cursor, 3, 8, 12, 16, 18, 22, 25
Apple
editing, 168-169 DATA, 131-144
keyboard, 2-7 debug, 80, 95-96
ASCII code, 178 Delete key, 3, 8, 12, 18, 22, 26, 162
Atari DIM (on Atari), 61-62
DIM with strings, 61-62
editing, 170 editing, 168-173
keyboard, 8-12 END, 93, 134, 137
equal sign (=), 79-80, 82, 83
BASIC erasing program lines, 35
language defined, 30, 36, 67 error messages, 165-167
programming rules, 38 entering input, 51
binary code, 178 OSS (Out of String Space), 72
bit, 178 out of data, 133
branching, 83 SYNTAX ERROR, 3-4, 13, 37
BREAK, 41-42, 53, 87, 163 Escape key, 6, 11
"bugs," 80-81, 95-96
byte, 178 FOR... NEXT, 114-130
formula, 54-56
Central Processing Unit (CPU), 175 for random numbers, 102-103, 105,
chips, 177 164
circuit board, 177 Function keys, 7, 16, 22, 24-25, 28
CLEAR (on TRS-80), 72
clearing the screen GOSUB... RETURN, 92-100
immediate mode, 4, 9, 13, 19, 23, 26, GOTO, 41-42, 44, 52
162 graphics, 15,27, 122-125
in a program, 68-69, 92, 162 greater than sign (>), 81, 83-84
colon, 80, 132, 166 Guess My Number (program), 108-
comma, 62, 70 111
Commodore
editing, 170-171 hardware, 175
keyboard, 12-17
Control key, 2, 5, 7, 10, 42 IF . . . THEN, 79-87
counters, 125-127 Texas Instruments, 85-86
counting loops, 114 ff. Timex Sinclair, 86-87
crazy letter (program), 70 infinite loop, 42
183
Index
quiz programs, 88-89, 96, 127-128, variables, use of, 48, 52, 61, 114, 125,
140-141, 145-154 132
QWERT (program), 145-149 VDT (Video Display Terminal), 176
184