Getting Started With Extended Color Basic
Getting Started With Extended Color Basic
.
.
. ..
.... .
..
.
~ ..
,
_.
-_
..
Getting Started with Extended Color BASIC: © 1984 Tandy Corporation, Fort Worth, Texas 76102 U ,5,A.
All Rigbts Reserved. Reproduction or use, without express written permission lrom Tandy Corporation, of any portion of this manual is prohlbited. While reasonable efforts have been taken in the preparation 01 this manual to assure its accuracy, Tandy Corporation assumes no liability resulting from any errors or omissions in this manual, or from the use of the information contained herein, JRS-80 Extended Color 8ASIC System Software: © 1984 Tandy Corporation and Microsoft. All Rights Reserved. The system software in the Color Computer is retained in a read-only memory (ROM) formal. All portions of this system software, whether in the ROM format or other source code form format, and the ROM circuitry, are copyrighted and are the proprietary and trade secret information of Tandy Corporation and Microsoft. Use, reproduction, or publication of any portion of this material without the prior written authorization by Tandy Cor" poration is strictly prohibited. 10987654321
stra ight
So spend a few hours with your computer. Type whatever you want. Play with it. Be bold and strange. In other words ... feel at ease! You have an amazing tool to command.
Customers • • •
Wei come back to the Co lor BAS IC fam i Iy! Let us in trod uce you to ... slight drum roll, please ... Extended Color BASIC. It has all the features of non-Extended Color BASIC plus much more. For example, with Extended Color BASIC you can: Edit a line Square a root a cube Playa symphony
Skip Section I except for Chapter 9. Chapter 9 shows how to use the Extended Co lor BAS IC "Ed itor" - a great ti me-saver in typ ing programs. Read Section II. You'll learn to use the most exciting features Extended Color BASIC - high-resolution graphics and music. Skip Section Ill. of
Read Section IV. This shows how to use the rest of Extended Color BASIC's expanded features.
BASIC
v.r.
(v.r. is two numbers specifying which version and release you have.) If you don't get this message: Turn the computer on and off again. Adjust the brightness and contrast on your television set. • Check all the connections.
If you still don't get this message, refer to "Troubleshooting and Maintenance" in Introducing Your Color Computer 2 or Introducing Your Deluxe Color Computer. Once you do get the above message, you're ready to start.
?
•
----IIII!Ii. __
CONTENTS
Section I THE BASICS
Chapter 1 Chapter 2 Meet your Computer. . . . . . . . . . . . . ... . . . . . . . . . ..
PRINT SOUND CLS
13
Your Computer Never Forgets (... unless you tu rn it off ... ) . . . . . . . . . . . Strings Variables See How Easy It Is?
NEW INPUT COTO RUN LIST IFflHEN
19
Chapter 3
_
PRINT, PRINT;
_.
24
Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter S Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13
30
35
40
Games of Chance
RND PRINT
, _. . . . . . . . . . . . . . . . . . . . . . . . . ..
43
47
Reading
DATA READ RESTORE
INT CLEAR
Writing.
EDIT
. . . . . . . . . . . . . . . . . . . . •. . . . . . . . . . . . . .. _. . . . . . . . ..
REM
53
60
DEL RENUM
Arithmetic
COSUB RETURN
. .....
65
71
A Pop Quiz
INKEY$ VAL
More Basics
STOP SCN CONT ABS MEM STR$ AND OR
75
Going in Circles
CiRCLE
Managing Numbers DIM Arrays Managing Words LUST PRINT#-2 String Arrays Sorting Analyzing Multidimensional Arrays
........ The Numbers Game . SQR SIN COS TAN ATN LOG Exr FIX OEF FN
It Don't Mean a Thing If It Ain't Got That String STRING$ INSTR MIO$ In One Door and Out the Other LINE INPUT PRINT USING ros A Little Byte of Everything LET TRON TROFF TIMER HEX$ Using Mach ine-l.anguage Subroutines USRn OEF USRn VARPTI~ Memory Map
171
32 33 34
ROM Routines
BASIC Summary Index
SECTION I
THE BASICS
In this section you'll learn how to program. Before you start, though, put yourself in the right frame of mind ... Don't try to do everything the "correct" way. Don't try to understand everything. Above all, please don't take- our word for anything! Do have fun with your Color Computer. Tryout your own ideas. Prove us wrong (if you can). Type anything and everything that comes to mind. Ready? Turn the page and begin.
CHAPTER 1
OK is the computer's "prompt." It's telling you, "OK, enough foolishness ... as soon as you are ready ... " (It patiently waits for your command.) You're the master-you tell the computer to do whatever you wish.
Give the computer your first command. Type this exactly as it is below: PRINT
"HI,
1'M
YOUR
COLOR
COMPUTER"
When you reach the right side of your screen, keep typing. The last part of the message appears on the next line. Now check your line. Did you put the quotation marks where we have them? If you made a mistake, no problem. Simply press the G key and the last character you typed disappears. Press it again and the next to the last disappears (... and so on and so on ... ). 13
PR I NT "H I ER"
I'M
YOUR
COLOR
CoMPUT
Press the (ENTER) key and watch. Your screen should look like this:
OK
PR I NT "H I t I'M YOUR COLOR COM PUT ER" HIt I'M YOUR COLOR COM PUlER
OK
Your computer just obeyed you by printing the message you have in quotes. Have it print another message. Type: PRINT "2" again obeys you and prints your next
2 +2
II
{ENTER)
2+2
You probably expect much more than an electronic mimic ... maybe some answers! Give your computer some numbers without the quotation marks. Type: PRINT 2 + 2 (ENTER)
a
14
The quotation marks obviously have a meaning. Experiment with them some more. Type each of these lines:
PR I NT 5+Ll (ENTER) PR r NT "5+£1" (ENTER) PRINT "5+4 EQUALS" PRINT 6/2 <ts 6/2" PR I NT ".8/2 (~NTER)
II
PR I NT 8/2
(ENTER)
RULES ON STRINGS
NUMBERS
The computer thinks of quotes as a journalist does. If the number's in quotes, the computer must PRINT it ex· actly as it appears. If it's not in quotes, the computer can interpret it by adding, su.b· treaing, multiplying, or dividing it.
The computer sees everything you type asstrings or numbers. If it's in quotes, it's a string. The computer sees it exactly as it is. If it's not in quotes, it's a number. The computer figures it out like a numerical problem.
-~
Do a multiplication
PR I NT 1588
problem:
23 (ENTER)
Notice that the computer's multipl ication sign is an asterisk (*), rather than the sign you use in math (X). The computer's so precise that it would get the X multiplication sign mixed up with the X alphabetical character. Try a few more problems:
PRINT 1'15 * 2 = " 15*2 (ENTER) PRINT 18 18 "IS THE SQUARE PR I NT 33,3/22,82 (ENHFll
DF 18"
(ENTER)
Notice how the computer handles parts in quotes v parts not in quotes.
Now it's your turn. Write two command Iines that print these two problems as well as their answers: 85
157/13.2= Ll3 =
DO-IT-YOURSELF COMMAND
LINES
15
_rd_,
command
prints on
rect" command line. For that matter, there is no correct way of handling your computer. There are many ways of getting it to do what you want. Relieved? Good!
P R I I NT"
The computer
HI"
(ENTER)
prints:
?SN ERROR ?S N E R RO R stands for" syntax" error. Th isi s the co m puter' s wa y of sa yi n g, "The co m m a nd 'PRII NT' is not in my vocabul ary ... I have no earth Iyidea what you want me to do." Any time you get the ?SN error, you probably made some kind of typographical mistake.
The com puter a Iso gi ves yo u erro r messages when it does understand what you want it to do, but it feels you're asking it to do something that is illogical or impossible. For instance. try this:
PR I NT5/0
The computer
(ENTER)
prints:
?/0
which
ERROR
ask me to divide by O-that's
means, "Don't
impossible!"
If you get an error message you don't understand, flip to the Appendix. We've listed all. the error messages there and what probably caused them.
Now your screen is a pretty shade of blue with a green stripe at the top. Your command told the computer to dear the screen and print color number 3-blue. If you don't get the right colors, refer to the color test in
introducrlng Your Color
Computer 2.
But why the green stripe? Whenever the computer prints characters, it must use a green background, not a blue background. Type some more characters. The computer uses a green background for them also. Co lors other tha n green are fo r pri n t ing pi ctu res. You' II learn how to do that later.
16
Press (ENTER) to get the OK prompt. Then type: CLS (7) (ENTER)
Now your screen is magenta (pinkish purple) with a green stripe at the top. Try some more colors. Use any number from 0 to 8. The Color Computer has nine colors. Each color has a numeric code. Type CLS without a number code: CLS (ENTER) If you don't use a number code, the computer assumes you simply want a clear green screen.
BUC: If you see a message saying MICROSOFT, or if you see a ?FCError message, you're using a number other than 0 through 8.
Two.
••
sou
N D 1 , 1 I2lrli (ENTER)
If you don't hear anything, turn up the volume and try again. What you're hearing is 6 seconds of the lowest tone the computer can hum. How about the highest tone? Type: SOUND 255, 1-\1)0(ENTER)
OK, so it has a good "hum-range" ... hope you're suitably impressed. Try some other numbers. Hope you like the computer's voice (it's the only one it has). You want to know what the other number is for? (Or maybe you've already found out.) The second number tells the computer how long to hum the tone. You can use any number from 1 to 255. Try 1: SOUND 128, 1 (ENTER)
BUC: Again, if you get a ?FC Error message, you're using a n umber other Iha n 1
The computer hums the tone for about 611OOthsof a second. Try 10: SO UND 128
I
10 (ENTER)
The computer sounds the tone for 6/1 Oths of a second. Try variations of both numbers, but keep in the range of 1 to 255.
....J!2!:£!I
h 255_
17
. .'
Curious about the reversed colors? Thev're lor people with a Color Computer 2 and a printer. The printer prints all "reversed" letters in lowercase. If you have a Deluxe Color
Pressthe (SHIFT) and CID (zero) keys, holding both down at the same time. Now release them and type some letters. The letters you type should be green on a black background. If they're not, try again, pressing (SHIFT) slightly before CID. Be sure to hold down both keys at the same time and then release them. Now, with the colors "reversed," command line: P R I NT The computer command.
II
HI
II
(ENTER)
understand
the
Press the (SHIFT) and CID characters again and release them. Type some letters. They should be back to normal: black with the green background. Press (ENTER) and type the same command line again. This time it works. The computer can't understand any commands you type with reversed col ors, If you ever press (SHIFT)(Q) by m i stake and fi nd yo u' re typ ing wi th these reversed colors, press (StIJlt)CID again to get the colors back to normal.
Learned in Chapter 1
BASIC WORDS KEYBOARD CHARACTERS (ENtER) CONCEPTS string v numbers
error messages
PRINT
SOUND CLS
A refresher like this is at the end of each chapter. It helps you make sure you didn't miss anything.
Notes
18
CHAPTER 2
NEVER
(...
off ... )
One skill that makes your computer so powerful "remember" the number 13. Type: A = 1 3 (ENTER]
is its "memory."
Have it
Now "confuse" the computer by typing whatever you want. When you're done, press (ENTER). See if the computer remembers what A means by typing: PR I NT A (ENTER]
or
or until
it prints
17.2.
memory:
in your computer's
You don't have to use the letter A. You can use any letters from A to Z. In fact, you can use any two letters from A to Z. Type: B = 1 5 (ENTER) C = 212)(ENTER) BC = 25 (ENTER]
19
Have it print all the numbers you've asked it to remember. Type: PRINTA,B,C,BC If you want the computer to remember a "string" of letters or numbers, use a letter with a dollar sign ($). Type: A$ = "TRY TO" B$ = "REMEMBER" C$ = "THIS, YOU" BC$ = "GREAT COMPUTER"
To the computer, a dollar sign means a string.
"Computer types" have a name for all the letters you've used: ables." So far, you've used these variables: YOUR COMPUTER'S MEMORY CHARACTERS
"vari-
A-17.2
A$-"TRY
TO"
B$- "REMEMBER"
C$-"THIS, YOU" BC$-- "GREAT COMPUTER"
Try to set the computer to remember a. letter we haven't used yet. What happens? Interesting. , .
Spot-check the above variables to see if the computer remembers the right information. For instance, to see if BC still contains 25, type: PR I NT BC (ENTER) Think of variables as little boxes in which you can store information. One .set of boxes is fa r stri ngs; the other set's for numbe rs. Each box has a IabeI.
As we said before, the computer has its rules and might get a fiale fussy with you if you don't play by them.
(ENTER)
TM stands for Type Mis· Match error. It means you didn't go by the rules.
The computer responds to both above lines with ?TM ERROR. It's telling you that you have to play by its rules.
20
To make the above lines obey the computer's rules, use a dollar sign with the D and Z. Type: 0'$ " "G" (ENTER) Z $ = "T HIS ISS TR I N G D A T A" (ENTER) The computer now accepts these lines.
How about this line? Do you think the computer accepts it?
o '$
= G (ENTER)
DATA A$
= =
G (ENTER) 12 (ENTER)
Now do something interesting with what you've asked the computer to remember. Type: PR I NT 0
2 (ENTER)
The computer prints the product of D times 2. Try this line: PRINT Z/D 21
of Z divided
by D.
cannot multiply
string data.
below that the computer
EXERCISE WITH F M
DZ$ M$
VARIABLES
= = = =
22.9998999 "19.2"
"REMEMBER 15 F+F TH I 5 FOR ME"
Finished?
the computer
accepts.
TH I 5 FOR ME"
RULES ON VARIABLES You may use any two characters from A to Z for a variable. The first character must be a letter from A to Z; however, the second may be either a numeral or a letter. If you want to assign it string data, put a dollar sign after it. Otherwise, it can hold only numeric data.
Learned in Chapter 2
CONCEPTS Variables String v Numeric Variables
learned how the computer thinks, it will be easy to write How about a break, though, before going to the next
Notes
23
CHAPTER 3
SEE HOW
Type: NEW (ENTER)
EASY IT IS?
This erases whatever may be in the computer's "memory." Now type this line. Be sure you type the number 10 first-that's 10 PR I NT
11
important.
HI,
I'M
Did you press (ENTER)? othing happened, did it? Nothing you can see, that N is. You just typed your first program. Type: . RUN [ENTER) The computer obediently runs your program. Type RUN again and again to your heart's content. The computer runs your program any time you wish, as many times as you wish.
Since this works so well, add another line to the program. Type: 20 Now type: L r S T (ENTER) Your computer obediently lists your entire program. Your screen should look exactly like this: 10 PRINT PUTER" 20 PRINT "HI, I 1M YOUR COLOR COM PRINT "WHAT IS YOUR NAME?" (ENTER)
What do you think will happen when you fun this? Try it. Type: RUN (ENTER) The computer prints: HI, 1'M YOUR COLOR COMPUTER WHAT I S YOUR NAME? 24
question
What? There's
When you simply type your name, the computer doesn't understand what you mean. In fact, the computer can't understand anything unless you talk
the computer
understands:
INPUT.
3 Q)
I N PUT A $ (ENlER)
it
If you make a mistake after pressing @ITID. simply type the line over again. .
This tells the computer to stop and wait fat you to type someth i ng, which label s as A$. Add one more line to the program:
£10
"HI
t"
A$ (ENTER I
LI ST (ENTER)
Your program should look like this:
"HI t I'M
RUN (ENlER)
Th at worked the program well, did n' t it? Th is is probabl y wh at ha ppened when you ran (depending on what you typed as your name).:
HIt I '.M YOUR COLOR COM PUTER WHAT I S YOUR NAME? ? JANE HI t JANE
RUN the program again using different names:
HIt I'M YOUR COLOR COMPU TER WHAT I S YOUR NAME? ? HUGO HI t HUGO HI t I'M YOUR COLOR COM·PUTER WHAT IS YOUR NAME? ? 772-36-8228 HI t 722-36-8228 HI t I '.M YOUR COLOR COMPUTER WHAT I S YOUR NAME? ? NONE OF YOUR BUS I NESS HI , NONE OF YOUR BUSINESS HI, I'M YOUR COL OR cnMPUTER WHAT I S YOUR NAME? ? I GET IT! ! HI t I GET IT I !
(The computer doesn't care what you call yourself.)
Here's what Line 30 did to your computer's memory each time you ran the program (assuming you gave it the same names we did):
25
There's an easier way to run your program over and over without having to type the RUN command. Type this line: 50 GOTD 10
'0
0000000000 O~
00000000000
I
..
o 0'
0
Now run it. The program runs over and over again without stopping. COTO tells the computer to go back to line 10: 1.0. 20
30
40 50
PR I NT "H I t I M YOUR PRINT "WHAT IS YOUR INPUT A$ PRINT "HI, "A$ GOlD 10
I
Your program now funs perpetually. Each time it gets to Line 50, it goes back to Line 1 O. We call this a "loop." The on Iy way you can stop th is endless loop is by pressing the (B_REAKJ key.
Change line 50 to give your name the attention it deserves. How do you change a program line? Simply type it again, using the same line number. Type: 50 10 20 30 ~40 \.50 GOTO 40 COLOR NAME?" COMPUTER"
I '''~:!~Wce
gram.
50 1m
This is what the program looks like now: PRINT "HI, 1'M YOUR PRINT "WHAT IS YOUR INPUT A$ PR I NT "H I ," A$ GOTD 40
Type RUN and watch what this loop does. When you've seen enough, press the (BREAK) ey. k There's a big change you can make simply by adding a comma or a semicolon. Try the comma first. Type Line 40 again, but with a comma at the end: 40 PRINT A$,
Run the program. The comma seems to print everything in two columns. Press (BREAK) nd try the semicolon. Type: a 26
all)
PRINTA$j
and run ... You probably won't be able to tell what the program's doing until you press (BREAK). See how the semicolon crams everything together?
Remember, if you make a mistake on one of the lines, ~~mply type the line over
~arn.
RULES ON PRINT PUNCTUATION This is what punctuation at the end of a PRINT line makes the computer do: 1, A comma makes the computer go to the next column, Use it to print in columns. 2. A semicolon makes the computer stay where it is. Use it to "cram" what you print together, 3. No punctuation makes the computer go to the next line. Use it to print in rows.
Color/Sound Demonstration
Want to play with color and sound some more? First, erase memory. Remember how? Then enter this program:
10 20, 30 40 50 PRINT "TO MAkE ME CHANGE MY TONE" PR I NT "TYPE I N A NUMBER FROM 1 TO 255" INPUT T SOUND T, 50 GOTD 113
T
NEW CftillJ!) . wish mine worked that easily! In this program we're using as a variable. However, we could use any letter.
L--
Notice that line 30 asks for T rather than T$. This is because we want numeric data rather than string data.
Know the answer? If you make the above change, the computer hums the same tone each time, but for a different length of time, depending on what number you use.
DO-IT-YOURSELF PROGRAM Press~ first and then erase this program by typing NEW. Now see if you can write a program, similar to the one above, to make the computer show a certain color. Remember, there are 9 colors, 0 through 8.
27
50
PR I NT "TO MAK E ME CHANGE MY COLOR" PR r NT "TY PE A NUMBER BETWEEN 0 AND 8" INPUTT CLS(T) GOTD 10
typing
---1'
Pressing the (BREAK) ey is a sloppy way to stop the program from running. k Why not have the computer politely ask if you're ready to end? Change Line 50 in the above program to: 50 PR I NT "DO YOU WANT TO SEE ANOTHER COLOR?"
THEN 20
Run the program. Type YES and the program. keeps running. Type anything else and the program. ends. This is what the program. looks like now: 10
e0
40
5~
6!tl 70
30
PR I NT "TO MAKE ME CHANGE COLORS" PR I NT "T'( PE A NUMBER BETWEEN 0 AND 8" INPUT T CLS (T ) PR I NT "DO YOU WANT TO SEE ANOTHER COLOR" INPUTR$ IF R$ = "YES" THEN 20
right
Line 50 prints a question. Line 60 tells the computer to stop and wait for an answer: R$. Line 70 tells the computer to go back to Line 20 IF (and only if) yo u r answer (R $) is' 'yes." If not, the program ends, since it has no more lines. You've covered a lot of ground in this chapter. Hope we're just whetting your appetite for more. Don't worry if you don't yet understand it perfectly. Just enjoy using your
computer.
Learned in Chapter 3
BASIC WORDS CONCEPT How to Change and Delete a Program Line KEYBOARD
IFfTHEN
28
Notes
29
CHAPTER 4
COUNT
THE. BEAT
In this chapter you'll experiment with computer sound effects. Before doing this, you need to teach the computer to count. Type:
10 2'0 30 40 FOR X '" 1 TO 10 PRINT "X = " X NEXT X PR I NT "I HAVE FIN
I SHED
COUNT I NG"
r;em,e,m,b'~"
I,~ p~n.;.;_
:VP, ~EW
Run the program a few more times. Each ti me, replace Line 10 with one of these lines:
'10 ~0 10 10 FOR X = 1 TO 100 FOR X = 5 TO 15 FORX,=-2TOZ FOR X = 20 TO ,24
Do you see what FOR and NEXT make the computer do? They make it count. Look at the last program we suggested you try:
10 20 30 40 FOR X = 20 TO 24 PRINT"X="X NEXT X PRINT "I HAVE FINISHED
COUNTING"
Line 10 tells the computer the first number should be 20 and the last number should be 24. It uses X to label all these numbers. Line 30 tells the computer to keep going back 10 Line 1a for the next number-the NEXT X-until it reaches the last number (number 24).
30
Look at Line 20. Since Line 20is between the FOR and NEXT lines, the computer must print the value of X each time it counts: = 20 X = 21 X '" 22 X = 23 X = 24 Add another line between FOR and NEXT: 15 PRINT" •••
COUNTING ••• "
v II
and run the program. With each count, your computer runs any lines you
choose to insert between FOR and NEXT.
DO-IT-YOURSELF PROGRAM 4-1 Write a program that makes the computer print your name 10 times.
DO-IT-YOURSELF PROGRAM 4-3 Write a program that pri nts the multiplication 9*25 .. tables for 9*1 through
HINT:
By adding a comma in the PRINT line, you can get all the problems and results on your screen at once.
Program 4- 1
1@ FOR X :: 1 TO i 20 PRINT "THOMAS"
Program 4-2
30
NEXT X
Program 4-3
10 FDR X" 1 TO 25
31
Counting
by Twos
Now make the computer count somewhat differently. Erase your program by typi ng NEW and the n type the origi naI program, usin g a new Line 10:
10 20 30
Ll,QI
FOR){ = 2 TO 10 STEP 2 PRINT "X= " X NEXT X P·R I NT "I HAIJE FIN I SHED
COUNT I NG"
Run the program. Do you see what the STEP2 does? It makes the computer cou nt by 2 s. line 10 te lis the com pu ter that: The first X is 2 The last X is 10 ... AND STEP 2 ... All the XSbetween 2 and 10 are two apart ... that is 2, 4,6,8, and 10. (STEP 2 tells the computer to add two to get each NEXT X) To make the com puter co unt by 3 s, make a.11 the Xs line 10:
10 FOR X = 3 TO 10 STEP 3
X=3 X=G
X You may be wondering about the programs you ran a1 the first of this chapter
It passes up the last X (number 1,0) because 9 + 3 = 12. Try a few more FOR ... STEP lines so you can see more clearly how this works: 1!ZJ
10 HI FOR X = 5 TO 50 STEPS FOR X = tel TO 1 STEP-1 FOR X = 1 TO 20 STEP Ll,
without using STEP. If you omit STEP, the computer assumes you mean STEP I.
10 (
20 30
40
Don't type the arrow, of course. It's there to help you understsnd.
This program makes the computer count from 1 to 255 (by 1s). Each time it counts a new number, it does what Lines 20 and 30 tel! it to do: Line 20-lt Line 30-lt For example: The first time the computer gets to FOR, in Line 10, it makes X equal to 1. Then it goes to Line 20 and prints 1, the value of X. Then Line 30 has it sound tone #1. Then it goes back to Line 10 and makes X equal to 2 Etc. What do you th ink the computer will do if you make this change to Li ne 10: 10
FOR){
= 255 TO 1 STEP-1
PROGRAMMING
EXERCISE
Using STEP, change Line 10 so the computer will sound tones from: (1) The bottom of its range to the top, humming every tenth note. (2) The top of its range to the bottom, humming every tenth note. (3) The middle of its range to the top, humming every fifth note. 10 ___ 10 10 ___ ___
Try this: To pause the prpgram while it's running, press the (mID.J and @ keys at the same time. Then press any key to continue.
DO-IT-YOURSELF PROGRAM 4-4 Now see if you can write a program that makes the computer hum: (1) from the bottom of its range to the top, and then (2) from the top of its range back to the bottom
33
Learned in Chapter 4
BASIC WORDS KEYBOARD Ct-iARACTER
FOR ...
TO ... NEXT
STEP
(SHIFTl(@)
Notes
34
CHAPTER 5
2!Zl
30
TO 8213"
Run the progra m. Be pati ent and wa it a (au pie of seeonds.Two seconds, to be precise. It lakes your computer two seconds to count to 920. Lines 1.0 and 20 set a timer pause in your program. By making the computer count to 920, you keep the computer busy for two seconds.
As you ean see, this is groundwork for a stopwatch. Erasethe program and
type: 10 213 30 40 50 PR NT HOW MANY SECONDS?" INPUTS FOR Z = 1 TO 46121*S NE.XT Z PRINT S "SECONDS ARE UP I!
II
I"
Run it. Input the number of seconds you want timed on your stopwatch.
DO-IT" YOU RSELF PROG RAM 5 - 1 It would help if the stopwatch could sound some kind of alarm. Add lines to the end of the program to give it an alarm.
35
II
S ARE UP! !!
SECONDS
IL
G0 70 80 80
FOR T ::: 120 TO 180 SOUND T, 1 NE>(T T FOR T ::: 150 TO 1110 SOUND T, 1 NE>(T T GOlD STEP
-1
10121
110 120
50
Notice the COTO line at the end of the program. It causes the message to keep printing and the alarm to keep ringing until you press (BREAK) r o (SHIFTli]ID.
3121
40
Notice the comma in Line 40. Try it without the comma. The comma makes "Y = " Y print on the next column.
5(1l
G0
Nf;:)<T Y
NEXT X
X=1
)0(
=2
X=3
Y=2
36
Call it a count within a count or a loop within a loop-whatever you prefer. Programmers call this a "nested loop." This is what the program does:
I.
It counts X from 1 to 3. Each time it counts X: A. B. It prints the value of X It counts Y from 1 to 2. Each time it counts Y: 1. It prints the val ue of Y
Whenever you put a loop inside another loop, you must close the inner loop before closing the outer loop:
@ ;:
10
FOR = 1 TO 3 FOR Y 1 TO 2
Right
40
NEXT Y NE>n x
€
t
Wrong 10 20
30 FOR X " 1 TO 3 FOR Y = 1 TO 2
NEXT
40
NE>(T Y
Making a Clock
With these tools, you can make the computer do much more. Type this: 10 20
30 FORS=0T059
PRINT
SOUND FOR T NEXT
S
150 2
=
T
1 TO 390
60
NE){T S
70
"1 MINUTE
IS,UP"
Run the program. This is what it does: I. It counts the seconds from A. B. It prints the second. It sounds a tone. It pauses long enough for one second to pass.
a to 59.
e.
II.
When it finishes counting all the seconds from 0 to 59, it prints a message that one minute is up.
37
There's a way to make this program look better. Add this line to clear the screen:
15 CLS
Now run the program. This time the computer goes through these steps: I. It counts the seconds from 0 to 59 (Lines 10 and 60). Each time it cou nts one second: A. B. C. It clears the screen (Line 15). It prints the second (Line 20). It sounds a tone (Line 30),
D. It pauses long enough for one second to pass (Lines 40 and SO).
II. When it finishes counting all the seconds from 0 to 59, it prints a message that one minute has passed (Line 70). it's easy to make a full-fledged clock:
TO 23 TO 58 TO 58
o =o
CLS
PRINT H": "M":
t
"S
60 70
80 80 100
=
5
1 TO 375
NEXT T
NEXT
NE>(T M NEXT H
110
Here's an outline of what the computer does in this program: I. It counts the hours from 0 to 23 (Line 10). Each time it counts a new hour: A. It counts the minutes from 0 to 59 (line 20). Each time it counts a new minute: 1. It counts the seconds from 0 to 59 (lines 30 and 90). Each
time it counts a new second:
a. b. c. d.
perpetual/y,
cora
B'y adding
It dears the screen (Line 40). It prints the hour, minute, and second (line 50). It sounds a tone (Line 60). It pauses long enough for one second to pass (Lines 70 and 80).
2. B. II.
When it finishes counting all the 59 seconds, it goes back to li ne 20 for the next minute (line 1DO).
Having a tough time with this program? Skip it for now, 11'/1 seem easy later,
When it finishes counti ng all the 59 minutes, it goes back to Line 10 for the next hour (li ne 110).
When it finishes counting all the hours (0-23), the program ends.
38
DO"IT-YOURSELF PROGRAM 5-2 Between Lines 90 and 100 you can add some tones that wi II sound each minute. Write a program that does this.
DO-IT-YOURSELF PROGRAM 5-3 Write a program that makes your computer show each of its nine colors for 1 second each.
Learned in Chapter 5
BASIC WORD CLS PROGRAMMING CONCEPT Nested Loops
Notes
39
CHAPTER 6
DECISIONS, DECISIONS
Here's an easy decision for the computer:
If you type "red"
•••
...
...
or ...
then make the screen blue
Easy enough? Then have the computer do it. Type this program: 10 20 30 40
Don't be confused by the arrows or the spac!,!s between program lines. We just put them in to illustrate the flow of the program.
PR I NT "DO YOU WANT THE SCREEN RED OR BLUE? INPUT C$ IF C$ = "RED" IF C$ = "BLUE" CLS(4) END CLS(3) THEN 100
II
l .~
~ .1
Run the program a few times. Try both "red" This is what the program does: If you answer "red" 1. 2. 3. ...
then ...
and "blue"
as answers.
Line 30 sends the computer to Line 100. Line 100 turns your screen red. Line 110 ends the program. (If the computer gets to Line 110, it never makes it to 200.) ... On the other hand . .
...
then
Line 40 sends the computer to Line 200. Line 200 turns your screen blue. Since Line 200 is the last line in the program, the program ends there.
What happens if you answer with something different from "red" "blue"? Run the program again. This time, answer "green." This makes the screen red. Do you know why?
HINT: If the condition is not true, the computer ignores the THEN part of the line and proceeds to the next program line.
or
PROGRAMMING
EXERCISE
There's a way to get this program to reject any answer but "red" or "blue." These are the two lines to add. You figure out where they go in the program:
I ~....RINT "YOU P
.... GOTO 20
"red.'
Answer: The Iines need to come after Li ne 40 and before Line 100:
50 60 PRINT "YOU GOTo 20 MUST TYPE EITHER RED OR BLUE"
DO-IT -YO U RSELF PROG RAM 6- 1 After the computer turns the screen red or blue, have it go back and ask you to type "red" or "blue" again.
HINT: You need to change Line 110 and add line 210.
iJ
"l
\)
30 40 50
60 100 110
"RED"
THEN THEN
'1'
C$ = "BLUE" "YOU 20 10 10
MUST
TYPE
cnro co ro
CLS(4) CLS(3)
200
210
cnr o
Trace the path the computer takes through this program. Go from one line to the next; follow the arrows where indicated. Notice the difference between the arrows going from the IFITHEN and the GOTO lines. 41
RULES ON IFfTHEN AND GOTO IFfTHEN is conditional. tion is true. GOTO is unconditional. The computer "branches" on Iyif the condi-
Although this chapter is short, you've learned an important programming concept. You'll have the computer make decisions all through this book.
Learned in Chapter 6
BASIC WORDS IF/THEN END
Notes
42
CHAPTER 7
GAMES OF CHANC.E
Thanks to a BASIC word called RND, the computer can play almost any game of chance. And even if you don't want to play computer games, you'll want to learn two words this chapter introduces: RND and PRJNT @. You'll also find in this chapter some more uses of IFffHEN. Type this program:
10 PRINTRND(lf2))
Run it. The computer just picked a random number from 1 to 1O. Run it some more times ... II's as if the computer is drawing a number from 1 to 10 out of a hat. The number it picks is unpredictable.
To make the eomouter pause -while running the program, press the (~I!IFT)
Type and run this next program. Press (BREAK) when you satisfy yourself that the numbers are random .. 10 20
PRINTRND(lQl)j GOTD 10
To get random numbers from 1 to 100, change Line 10 and run the program. 10 P·RINTRND(100);
How can you change the program to get random numbers from 1 to 255?
" •• ~+ ••
~+ ••
+r.
'~~r.'
•••••••
,.~••••••••••••
> •••••••••••••••
rr.'
••••••••
~~.,.,.,
•••••
~+ ••••
+~ ••
~r.""
or.,
• ,.•••
10
RND(255)
A Random Show
Justfor fun, have the computer compose a song made up of random tones. Type:
10 ~ 30
20
10
43
Run it. Great music, eh? Press (BREAK) when you've heard enough.
:.neak preview: Enioying graphics and sound? Go ahead and tryout some programs in Section 1/, "Sights and Sounds."
DO-IT-YOURSELF PROGRAM 7-1 Add some lines to make the computer show a random color (1-8) just before it sounds each random tone.
Here's our program: 10 1£1 18 ( 20 30 T = RND (255) C = RND(S) CLS(C) SOUND Ttl corn 10
We have a few simple games in this chapter. Feel free to use your imagination to add interest to them-or invent your own.
Russian Roulette
In this game, a gun has 10 chambers. The computer picks, at random, which of the 10 chambers has the fatal bullet. Type:
Remember a/ways to type NEW (]HITID before entering a new program.
10 20 30 t!0 50 80 100
PRINT "CHOOSE YOUR CHAMBER( 1-10)" INPUT X IF){ = RND( 10) THEN 100 SOUND 200, 1 PRINT "--CLlCK--" cnro 1 QI PRINT "BANG--YoU 'RE DEAD"
First, in Line 20, the player inputs X (a number from 1 to 10). Then, the computer compares X with RND(10) (a random number from 1 to 10). Then it follows the
If
arrows":
'"
If X is equal to RND(l 0), the computer goes to Line 100, the "dead
routi ne."
Remember how to list part of LI ST 50-130 lists the program's middle part.
Make the dead routine in Line 100 better. Type: 100 ( 120 130 1£10 150 160 170
a program?
110 PR I NT
Try this when listing. a long program: At the start of the listing, press ootIm and (@). This causes the listing to pause. Then press any key to continue.
FOR T = 133 TO 1 STEP -5 BANG! ! ! ! ! SOUND T I 1 NEl<T T CLS PRINT@230. "SORRY, YOU'RE DEAD" SOUND 1.50 PRINT @ 380, "NE}{T VICTIM, PLEASE"
II II
Run the program. Here's what the routine does: Lines 100-130 make the computer sound descending tones and print BANG!!!!! over and over again on the screen. 44
Line 140 clears the screen. Since no color is given, the computer makes the screen green. Lines 150 and 170 use a new word-PRINT @-to position two messages on your screen: SORRY, YOU'RE DEAD and NEXT VICTIM, PLEASE. The grid below shows the 511 positions on your screen. Line 150 prints SORRY,YOU'RE DEAD at position 230 (224 + 6). Line 170 prints NEXT VICTIM, PLEASEat position 390 (384 + 6).
1 2)
A t~,
..
'"
" v,
"P RIN T @ 5creen Localions." Use it to plan your programs' screen formats,
DO-IT-YOURSELF PROGRAM 7-2 Change this program so that if the player does manage to stay alive for 10 clicks, the computer pronounces the player the winner, printi ng th is message on the screen:
·q-:'I-H-H++H1I-H-++++t+lrtf-++++t+iH-t-+++ ::I+I+II-H-H++t-HH-++fH1t-HH-++ttf-I-Hf+
HINT:
You can use the FOR/NEXT loop, so that the computer can keep. count of the number of clicks,
45
CLS
1I0
'It
II)
...
~
II
50 80 70
80
""
80
100
X = RNO(G) Y = RNO(8) R ,., + 'I X PRINT @ 200,}( PRINT.@ 2111, 'I PRINT @ 3811, "YOU ROLLED A" R PRINT @ 1I511, ''~O YOU WANT ANOTHER INPUTA$ IF A$ = "YES" THEN 10
ROLL
?"
Run the program. li ne 10 dears the screen. Line 20 picks a random number from 1 to 6 for one die. Line 30 picks a random number for the other die. Line 40 adds the two dice to get the total roll. Lines 50-70 print the results of the roll. Line 90 lets you input whether you want another roll. If you answer "yes," the program goes to Line 10 and runs agai n. Otherwise, since this is the last line in the program, the program ends.
DO-IT-YOURSELF PROGRAM 7-3 Since you know how to roll dice, it should be easy to write a "Craps" program. These are the rules of the game (in its simplest form): 1. The player rolls two dice. If the first roll's a 2 ("snake eyes"), a 3 ("cock-eyes"), or a 12 ("boxcars"), the player loses and the game's over. 2. If the first roll's a 7 or 11 ("a natural"), game's over. the player wins and the
3. If the first roll's any other number, it becomes the player's "point." The player must keep rolling until either "making the point" by getting the same number again to win, or rolling a 7, and losing. You already know more Make the computer print keep the player informed while to finish, but give than enough to write this program. Do it. it in an attractive format on your screen and about what is happening. It may take you a it your best. Good luck!
learned in Chapter 7
BASK WORDS RNO PRINT @
Notes
46
CHAPTER 8
READING
Your computer is a natural at teaching. It'~ patient, tireless, and never makes a mistake. Depending on the programmer (you, of course), it also can be imaginative, consoling, and enthusiastic. Using RND, have it teach you math. Type:
10
Are your programs gelling long? If you have a cassette recorder, read your computer's introduction manual to learn how to save your pro" grams on tape. If you have a Deluxe Color Computer, you can also save programs jn memory. See your introduction manual to learn how.
20
30
1I0
?n
PRINT "THE ANSWER IS" X*V PR I NT "BETTER LUCK NEXT T I ME" GOTO 100 PRINT "CORRECT
II
105 110
The above program drills you on the multiplication and checks your answers.
DO-IT-YOURSELF PROGRAM 8-1 Make the program drill you on addition problems from 1 to 100.
47
45 50
60
+Y
Make the program more interesting. Have it keep a running total of all the correct answers. Type:
When you first turn on the computer, all numeric variables equal O. When you type NEW cmIml, all numeric variables also equal 0.
15 95
98
T=T+1 C" C + 1
PRINT "THAT "ANSWERS" IS" C "CORRECT OUT OF" T
T is a "counter." It counts how many questions you're asked. When you first start the program, T equals zero. Then each time the computer gets to Line 15, it adds 1 to T. C is also a counter. It counts your correct answers. Since C's in Line 95, the computer doesn't increase C unless your answer's correct.
DO-IT-YOURSELF
PROGRAM 8-2
Make the program more fun. Have it do one or more of the following: 1. Call you by name.
PRINT @ for this.) 4. Keep a running total of the percentage of correct answers. 5. End the program if you get 10 answers in a row correct. Use your imagination. We have a program in back that does this all.
ORANGES
PEARS
FOR X = 1 TO 3 READ F$
NE}(T X
What happened ... nothing? Nothing that you can see, that is. To see what the computer is doing, add this line and run the program: 35 PRINT "F$
= :"
F$
Line 30 tells the computer to: 1. 2. Look for a DATA line. READ the first item in the list~APPLES.
48
3. 4.
The second time the computer gets to Line 30it 1. 2. 3. 4. Look for a DATA line. READ the first item-this
When you run the program, this happens in the computer's memory:
2
3
What if you want the computer to read the same list again? It's already "crossed out" all the data ... Type:
60
GOTo 10
"out of
Runthe program. You get an error: ?OD ERROR IN 30. Ofrmeans data." The computer's crossed out all the data. Type this line and run the program:
5rli RESTORE
Now it's as if the com puter never crossed out any data. Itread sthe same list again and again. You can put DATA lines wherever you want in the program. Run each of these programs. They all work the same.
Remember
com.puter p.ause W.hile run" ning a program! Press (SHIFT! (@J 10 pause and any key to gel it to continue.
10
C C
51il 60
20 ~0
70
DATA APPLES DATA ORANGES FOR x = 1 TO 3 READ F$ PRINT "F$ = : NEXT X DATA PEARS FOR X - 1 TO 3 READ F$ PRINT "F$ = : NEXT X DATA APPLES DATA ORANGES DATA PEARS
10
20 ( 30
II
1I0
F$
50 \60
f30 1I0
DATA APPLES , ORANGES DATA PEA.RS FOR X = 1 TO 3 READ F$ F$ PRINT "F$ = NE){T x
: ,II
~Iil
50 80 70 80
81il
II
F$
50 60
70
-- -
49
Definitions
a0
S0
DATA TACITURN, HABITUALLY UNTALKATIIJE D A TAL 0 CJ U A C IOU S I V E R Y TAL K A TI I) E DATA VOCIFEROUS, LOUD AND I)EHEMENT DATA TERSE, CONe I BE DATA EFFUB I VE I DEMONSTRAT I VE OR GUSHY
Now get the computer to select one of these words at random. Hmmm ... there are ten items. Maybe this works:
60
70
80 80 100
11
WORD IS:
11
A$
Run the program a few times. It doesn't work quite right. The computer's just as likely to stop at a definition as at a word.
What the computer really needs to do is pick a random word only from items 1, 3, 5, 7, or 9. Fortunately, BASIC has a word that helps with this. Type:
6S
IF INT(N/2)
N/2
THEN
N=N- 1
Now run the program a few times again. This time, it should work. INT tells the computer to look at only the "whole part" of the number and ignore the decimal part. For instance, the computer sees INT(3.9) as 3. Assume N, the random number, is 10. The IF clause in Line 65 does this:
INT(10/2) INT(S)
=S
10/2
5 '" 5 The above is true: 5 does equal 5. Since it's true, the computer completes the THEN clause. N is adjusted to equal S (10 - 1 ) . Now assume N, the random number, is 9. The IF clause in line 65 does this:
INT(S/2) INT(LI.5)
a so
a.
= =
5
9/2 a.s
The above is not true: 4 does not eq ua I 4.5. Si. ce it's not true, the co mputer n doesn't complete the THEN clause. N remains 9. Besides reading a random word, the computer also must read the word's definition. Add these lines to the end of the program: 110 i ze
READ B$ PRINT '''THE DEFINITION IS :" B$
Now run the program a few times. Have the computer print one random word and definition after the next. Add th i 5 to the start of the program:
5 CLEAR 100
This reserves plenty of "string space." Add these lines to the end of the program:
130
llj0
RESTORE GoTD G0
This lets the computer pick a new random word and its definition from a "restored" group of data items. Here's how the program now looks:
5 1 r.ll 20 30 lj0 CLEAR 100
50 60 65 70
1- ~: ( 100
11 0 \ 130 \ 1 a0
120
DATA TAC ITURN , HAB ITUALL Y UNTALKAT II,)E DATA LOQUACIOUS, VERY TALKATgJE DATA VOCIFEROUS, LOUD AND l)EHEMENT DE'lTA TERSE, CONC I SE DATA EFFUSIVE, DEMDNSTRATI\,JE OR GUSHY N " RND ( 10) IF INT(N/2) '" N/2 THEN N '" N - 1 FOR X '" 1 TO N READ A$ NEXT X PR I NT "A RANDOM WORD IS: " A$ READ B$ PRINT" ITS DEFINITION IS r" B$ RESTORE GOTD Gil:!
~~'~
and capitals, cities and
DO-IT-YOURSELF PROGRAM 8-3 Want to complete this program? Program it 1. Prints the definition
50
only.
2. Asks you for the word. 3. Compares the word with the correct random word. 4 _ Tell syou if you ran swer is correct. If your answer is incorrect, pri nts the correct word.
51
Here's our program: 5 CLEAR 51313 DATA TACITURN, HABITUALLY UNTALKATIl..lE 10 20 DATA LOQUACIOUS I VERY TALKATII)E LOUD AND I)EHEMENT 30 DATA VOCIFEROUS, 40 DATA TERSE, CONe I BE 50 DA T A· E F F UBI V E , D EM 0 NS T RAT I V E 0 R GUS HY N=RND(1ilJ) }'f60 IF INT<N/2) = N/2 THEN N = N - 1 65 FOR>(=1TON 70 READ A$ 80 NE}(T X 813 READ B$ 110 PRINT "WHAT WORD MEANS:" B$ 1213 RESTORE 1313 INPUT R$ 140 IF R$ = A$ THEN 1813 1513 1613 PR I NT "WRONG" 170 PRINT "THE CORRECT WORD IS :" A$ GoTO 60 1813 PRINT "CORRECT" 1813
2013
co r o
GQl
Learned in Chapter 8
BASIC WORDS DATA READ RESTORE INT CLEAR
Notes
52
CHAPTER 9
WRITING
Up to now, you've probably been changing programs the long and boring way-by retyping them. If so, you'll be glad you've arrived at this chapter. You'll learn a new, easy way to change programs-by "editing" them.
Edit It!
(EDIT)
Pretend you make a mistake typi ng a program. Line 50 somehow ends up:
50 DABA EFFFUSIVE,
GIMPY
MUSHY
or the easy way, by
Youcan change this line the hard way, by retyping itediting it. To get into Line 50's "edit mode," type: EDIT You see: 50
50 (ENTER) GIMPY
50 DABA EFFFUSIVE,
MUSHY
You're now in the edit mode. While in this mode, you can use any of the special "edit keys" to display or change Line 50. They're all listed later in 'this chapter (Table 9.1). Startby pressing CD, the edit key for "list." The OJ key displays the entire line again and then pu ts you back at the start, MOVE ON DOWN THE LINE (CURSOR MOVEMENT) Press (SPACEBAR) a few times. This key moves you forward. To move backward, press 8. Note that while in the edit mode B merely backspaces; it doesn't delete characters.
53
Move to the start of Line 50 and presstd) (SPACEBARl. This moves you five spaces forward - a II a t once. Do the same wi th 8. Pressa number, suc h as 00, and B and move that many spaces backward. Move to the start of Line 50 and press (for "search") a nd then CD (the character for which you want to search). This moves you 10 the first E. Move back to the start and press CD. Th is moves you to th e second E in line 50.
rn m
CHANGE THE LINE (CHANGE) Make your first change to Line 50. Change DABA to DATA:
Once you enter the edit mode, you don't have to
press (ENTER) after 5Vbcom-
character -
the B in DABA.
Type the new character, in this case, T. To be sure the change is made, press CD and you see:
50 DATA
EFFFUSIVE!
GIMPY
MUSHY
Now make the next change: Change GIMPY 10 GUSHY. This time you'll change three characters at a time: Move to the first wrong character Press 00 the I in GIMPY.
m for
50 DATA
EFFFUSIVE.
GUSHY
MUSHY
If th i s were aII you needed to do to line 50, you cou Id press (EN! ER) and get
out of the edit mode. As you can see, though, you have much more work to
do.
YOU'RE OUT! (DElETE) You need to delete a character one of the F'sin EFFFU$IVE: the third F in EFFFUSIVE.
m again:
MUSHY
50 DATA EFFUSIVE,
GUSHY
You can del ete more tha none ch aracter at a ti me. For exam pi e, if you press @) 00, you'll delete four characters at a time.
SQUEEZE IT ALLIN
(INSERT)
You now need to insert some characters: GUSHY should be DEMONSTRATIVE OR GUSHY, Move to where you want to insert characters the Gin Gushy. Press CD for "insert mode." Type your insert DEMONSTRATIVE OR the space before
If you oress CD (0 list the line while using insert, you'll insert the letter "L" into Ihe program line instead of ll.isfing the line.
At this point, you're still in the insert mode. For example, if you press (SPACEBAR),you'll insert a blank space; if you press (TI, you'll insert an l. Therefore, you need to: Press (SHIFT1CIJ Now you can press to get out of the insert mode, list the line:
---~-
OJ to
DEMONSTRATIVE
OR GUSHY
HACKAMORE OR HACKALESS? (HACK) With "hack" you alter (halter?) a line by hacking the end ofit and inserting new characters. Try hacking at Line 50: Move to the first character you want hacked off-
Press for hack. This hacks off the rest of the line and puts you in the insert mode. Type your insert in this case, type CRUSTY.
ao
the M in MUSHY.
Press (SHIFT)(]J to get out of the insert mode. If you list the line now (by pressing
DEMONSTRATIVE
Kill
THE ...
AH •..
MISTAKE (KilL)
Kill is almost the opposite of hack. It "kills" everything up to the nth occurrence of a character. Suppose that, just for kicks, you want to kill the first half of Line 50-everything up to the comma. Move to the startof Line 50 and press these keys:
ooGJ
If you list Line 50 now, you see:
50
,DEMONSTRATI\.JE OR GUSHY
CRUSTY
EXTENDED
COLOR
(EXTEND)
Press00 for exten d. Th e cu rso r maves to the end of a nd you enter the insert mode. Type your insert: AND MUSHY • Press (SHIFT)rn to get out of the insert mode.
Line 50 is now:
50
,DEMONSTRATIVE
OR GUSHY
CRUSTY
AND MUSHY
BASIC uses L)
Key
Action Lists the line and moves to the start. Changes the next n characters to new characters. Inserts characters. Deletes n characters. "Hacks" the rest of the line and puts you in the insert mode. Lets you extend the line Searches for the nth occurrence of character. Kills rest of line. Ki If 5 (deletes) up to the nth occurrence of character. Moves n spaces forward. Moves n spaces backward.
CD
n©c ha r ecters
n(J[}
CD
un
00
nOOch a racter
00
nG
56
Mass Delete
(DELETE)
Up to now, you've deleted lines the simple way, like this:
50 (ENTER)
Thisworks fine for one or two Iines, but what if you want to delete 50 or 60 lines?You may find it easier to start over. Extended Color BASIC comes to the rescue again with an easy way to delete program lines - the DEL command. For instance, if you want to delete Lines 30~50, type: DEL3Ql5!2l (ENTER)
So now you can change everything about a program line except the line number itself. Well, despair no more, because you can even do that with
RENUM.
IS THE
IS
THE
10
Listthe program and you see the new line numbers beginning with 100. Line 1 00 is what we ca II th e new li ne:
r ie
120 130
100
IS IS
Goro
120
(ENlER)
Here, the newline is 200, but the renumberi ng starts with Line 120. line 120 is what we call the start/ine:
1!2l0 110 200 210
IS IS
'8
57
Renumber the program one more time giving it an increment of 50 between each line: RENUM 300, ,50 (ENTER)
Here the newl ine is 300. Since you om itted the startl ine, BASIC ren umbers the entire program. The increment between the lines is 50: 300 350
LlQl0
450
IS IS
RENUM newline, startline, increment Renumbers a program. newline is the first new renumbered line. If you omit newline, BASIC uses 10. startline is where the renumbering starts. If you omit startline, BASIC renumbers the entire program. increment is the increment between each renumbered line. If you omit increment, BASIC uses 10. Note: RENUM does not rearrange the order of lines.
Th is ren umbers yo ur enti re progra m. The new line is 10, and the increment is 20: 10 PRINT 30 PRINT 50 PRINT 70 GOTO "THIS "THIS "HERE'S 10 IS IS THE FIRST LINE" THE SECOND LINE" ANOTHER LINE"
Type RENUM 40,30, (ENTERl. Here, the newline is 40; the stsrtlin« is 30; and the increment is 10: "THIS PRINT "THIS 5.0 PR I NT "HERE'S 60 GOTD 10
Ll0
10 PRINT
IS IS
Type RENUM 5,40 (ENTER) and you get a ?FC Error, This is because the result would move Line 40 ahead of line 10.
Learned in Chapter 9
BASIC WORDS
EDIT RENUM
DEL
58
Notes
59
CHAPTER 10
ARITHMETIC
Solving long math problems fast and accurately is a task your computer does with ease. Before typing long, difficult formulas, though, there're some shortcuts you'll want to use. An easy way to handle campi icated math formulas is with "subroutines." Type and run this program:
;~
413
113
PR I NT "E)<ECUT I NG THE MA I N PROGRAM" / GOSUB 500 PR I NT "NOW BACK I N THE MA I N PROGRAM" END PRINT "EXECUTING RETURN THE SUBROUTINE"
(O/W) -
500
510
Ax (BY
+ C) - D + E
COSUB 500 tells the computer to go to the subroutine that starts at Line 500. RETURN tells the computer to return to the BASIC word that immediately follows COSUB. Delete Line 40 and see what happens when you run the program.
If you did this, your screen shows: EXECUT I NG THE MA I N PROGRAM EXECUT I NG THE SUBROUT I NE NOW BACK IN THE MAIN PROGRAM EXECUT I NG THE SUBROUT I NE ?RG ERROR IN 510 RC means "RETURN without GOSUB./1 Do you see why deleting END in Line 40 causes this error? At first, the program runs just as it did before. It goes to the subroutine in Line 500 and then returns to the PRINT line that immediately follows COSUB. Then, since you deleted END/it goes to the next line-the subroutine in Line 500. Th is time, though, it doesn't know where to return. This is because it's merely "dropping" into the subroutine; it is not being sent to the subroutine by a GOSUB line.
60
10
cnrn 10
2010 2!Zl20 2Ql3!Zl 2!ZlLl0 205!Zl 2060
E
INPUT "TYPE A NUMBER" j N INPUT "TYPE THE POWER yOU WANT IT RAISED TO" ; P GDSUB 2000 PRINT: PRINT N "TO THE POWER OF" P "IS" E. REM FORMULA POWER FOR X:;;
E ::: E
FOR RA I SING
A NUMBER
TO A
See something different about INPUT? You can have the computer print a. message before waiting for your input.
TO P
N
Also introduced in this program are: The colon (:},in Line 40. You can combine program lines using the colon to separate them. Line 40 contains the two lines: PRINT and PRINT N "TO THE" P "POWER IS" E. REM, in Line 2000. REM means nothing to the computer. Put REM lines wherever you want in your program to help you remember what the program does; they make no difference in the way the program works. To see for yourself, add these lines and run the program: 5 REM TH I SIS A PECULI AR PROGRAM, 17 REM WILL TH I S LI NE CHANGE THE PROGRAM? L15 REM THE NEXT LINE KEEPS THE SUBPROGRAM SEPARATED
PRINT by itself tells the com" psner 10 skip a line.
Change the above program so that the computer prints a table of squares(a number to the power of 2) for numbers,say,from 2 to 10.
+
2
3 by 8
You may want the computer to arrive at the answer this way: 13
3/8
16/8
But, instead, the computer arrives at another answer. Type this command line and see: PR I NT 1 3 + 3 I 8 (ENTER) 61
RULES ON ARITHMETIC ~
An "operation" is a problem you wan! the computer to solve. Here the operations are addition, subtraction, multiplication, and division.
1. First, it solves any rnuItipiicationan d division operations. 2. Last, it solves addition and subtraction operations. 3. If there's a tie (that is·,more than one rnultiphcatiorvdivlsion or
In the problem above, the computer follows its rules: First, it does the division (3/8 Then, it does the addition (13
= .375) + .3 75
:=:
13.375)
For the computer to solve the problem differently, you need to use parentheses. Type this line: PR I NT (1 3 + 3) / 8 {ENTER)
Whenever the computer sees an operation in parentheses, it solves that operation before solving any others.
COMPUTER
MATH EXERCISE
What do you think the computer will print as the answersto each of these problems?
PRINT 10 - (5 - 1) /2 _ _ _
_
PR I NT 10 - 5 - 1 / Z PRINT (10 -5 - 1) /2
PR I NT (10 - 5) - 1 / 2
PRINT 10 (5 - 1 /2)
Finished? Type each of the command lines to check your answers. What if you want the computer to solve this problem? Divide 10m inus the difference of 5 minus 1 by 2 You're actually asking the computer to do this: (10-(5-1))/2 When the computer sees a problem with more than one set of parentheses, though, it solves the inside parentheses and then moves to the outside parentheses. In other words, it does this:
62
no ~ no -
(5 ~
1)) /
4) / 2
---+)
5- 1= 4 10 ~ 4 = 6 6I2 = 3
6 / 2 \.._-----~)
t.._
~>
RULES ON PARENTHESES
1. The computer solves operations enclosed in parentheses llrst, before solving any others.
2, The com puter solves the innermost parentheses fi rst. I! then works
MATH EXERCISE
30 - S - 8 - 7 - G
Answer:
PRINT30(8(8<7-8»)
Saving Routines
Theprogram below uses two subroutines, It's for those of you who save by putting the same amount of money in the bank each month:
10 20 30
1I(1l
YOUR MONTHL Y DEPOS IT" j D BANK'S ANNUAL I NTEREST RATE" NUMBER OF DE PO SITS" jP 10Q10 "YOU WILL HAI,IE $" FI,I "IN"
I=I112*.01
P "MONTHS"
1000 REM COMPOUND MONTHL Y I NTEREST FORMULA 1010 N=1+I 1020 GDSUB 2000 1030 nJ=D*«E-ll/Il 101'Hll RETU.RN Z00Q1 REM FORMULA FOR RA I 5 I NG A NUMBER TO A
POWE,R
E= 1 FOR X = 1 TO P
E=E N NEl<T X IF P := 0: THEN RETURN
E=1
63
[ SA.NK
Notice that one subroutine "calls" another. This is fine with the computer as long as: there's a GOSUB to send the computer to each subroutine, and there's a RETURN at the end of each subroutine. Turn to the Appendix, "Subroutines." you can add to your programs. You'll find useful math subroutines
Learned in Chapter 10
BASIC WORDS
GOSUB
BASIC SYMBOLS
()
BASIC CONCEPTS
Order of operations
RETURN
REM
Notes
64
CHAPTER 11
Combining Words
Type and run this program:
10 20 PRINT "TYPE A SENTENCE" INPUT 5$ PRINT "YOUR SENTENCE HAS" CHARACTERS" INPUT "WANT TO TRY ANOTHER" IF A$ = "YES" THEN 10
30
40 50
LEN(S$)
j A$
Impressed?LEN(S$) computes the length of string S$-your sentence. The computer counts each character in the sentence, including spaces and .punctuation marks.
Erase the program and run this, which composes a poem (of sorts):
10 2e1 A$ = "A ROSE" B$ = " " C$="ISAROSE" D$ = B$ + C$ E$ = "AND SO FORTH AND SO ON F$ A$ + D$ + 0$ + B$ + E$ PRINT F$
30
40 50 60 70
11
Herethe plus sign (+) combines strings. For example, D$ ("IS A ROSE") is a combination of B$ + C$. Thereare two problems you may encounter when combining strings. Add thefollowing line and run the program. It shows both problems:
80 G$
F$ + F$ + F$ + F$ + F$ + F$ + F$
You will nOI gel the error if you have not started up your computer since you ran the program from Chapter 8 with the CLEAR 500 line.
as
Whenthe computer gets to Line 80, it prints the first problem with this line: IDS ERROR IN 80 ("out of string space").
65
On startup, the computer reserves only 200 characters of space for wofking with strings. line 80 asks it to work with 343 characters. To reserve room for this many characters and more (up to 500), add this line to the start of the program and run: 5 CLEAR500
Now when the computer gets to Line -80, it has enough string space, but prints the second problem with this line: ?lS ERROR IN 80 ("string too long"). A string can contain no more than 255 characters. When storing more than 255 characte rs. you need to pu t these characte rs into severaI stri ngs.
Twisting Words
Now that you can combine strings, try to take a string apart. Type and run th is progra m : 10
20 30 40 NO! impressed? tater, we'll show practical U5e5 this unusual skill.
INPUT "TYPE AWDRD" j W$ PRINT "THE FIRST LETTER PRINT "THE LAST 2 LETTERS (W$ t 2) GOTO 10
IS
or
Here's how the program works: In line 10 you input string W$. Assume the string is MACHINE:
0°
In lines 20 and 30, the computer computes the first left letter and the last two· right letters of the stri ng: MAC LEFT$ (W$ tl. ) H I NE RI GHT$ (IH. 2)
Run the program a few more times to see how it works. Now add this line to the program: 5 CLEAR
50Q1
so that your computer will set aside plenty of space for working with strings. Run the program again. This time input a sentence rather than a word. PROGRAMMING EXERCISE
How would you change Lines 20 and 30 so that the computer will give you the first 5 letters and the last 6 letters of your string? 20~~~~~~~~~~~~~~~~~~~~ 30 ~ __ __
Answers:
20
FIRST
FIVE
LETTERS
ARE:"
LEFT$
66
3121
PRINT (1.1$
.s :
"THE
LAST
SIX
LETTERS
ARE·"
RIGHT$
20
3121 40 50 G0 70 80
CLEAR 500 INPUT "TYPEA SENTENCE" j S$ PRINT "TYPE A NUMBER FROM 1 TO " LEN (S$) INPUT)« PRINT "THE MIDSTRING WILL BEGIN WITH CHARACTER" PR I NT "TV PEA NUMBER FROM i TO " LEN (S$)
NEW (.m!ID
-X
+1
INPUT Y PRINT "THEMIDSTR'ING "CHARACTER'S LONG" 9121 PRINT "TH!S MIDSTRING 10121 GOTO 20 WILLBE" IS :" Y MID$(S$
.x
IY)
Run this program a few times to see if you can deduce how MID$ works. Here's how the program works: In Line 20, assume you input HERE IS A STRING:
YOUR COM PUTER 'S M.EMOR'{
S$ --
HERE
I S A STR I NG
In Line 30, the computer f rst computes the length of 5$, which is 16 characters, It then asks you to choose a number from 1 to 16, Assume you choose 6. InLine 60, the computer asks you to choose another number from 1 to 12 116~6+1). Assume you choose 4.
X-G Y-4
InLine 90, the computer gives you a "mid-string" character and is four characters long:
HE
2345 RE
G 7 8 8 10 11 IS A S -4_
MID$(S$IG 14)
12 T
13 R
For another example of MID, erase the program and run this:
10 20 30 INPUT "TYPEA SENTENCE" j S$ I N PUT" TV PE A WORD I N THE SENTENCE ,. j W$ L= LEN (W$ ) FOR = 1 TO LEN (S$ ) IF MIO$(S$IXIL) = [.1$ THEN 80 NEXT X PR I NT "YOUR WORD I SN TIN THE SENTENCE" END PR I NT .W$ "- -BEG I NS AT CHARACTER NO. " X
40
50
Gtl! 7il! 8121 80
You can use thi5 kind of program to sort through inI formation. For instance, by 5eparMing strings, you could look through a mailing list ,-for TEXAS addre5SeS.
67
Here's
In Line 20, assume you input the word counts W$'s length: 2 characters.
In Lines 40-90 (the FOR/NEXT loop), the computer counts each character in S$, starting with character 1 and ending with character lEN(S$j, which is 16. Each time the computer counts a new character, it looks at a new midstring. Each mid-string starts at character X and is l (2) characters long. For example, when X equals
1,
A
the computer
MID$(S$
--2-
HER
E
,1.2)
STRING
looks at
th is rnid-stri ng:
HER
£I
STR ING
it is
6, the
computer
finally
for which
10
A$ = "CHANGE
A SENTENCE."
Add a line that inserts th is to the start of A$: IT'S EASY TO Add another line that prints the new sentence: IT'S EASY TO CHANGE A SENTENCE
68
20
30 £10
+"
DO-IT-YOURSELF PROGRAM 11-2 Add to the above program to make it: • Find the start of this mid-string: A SENTENCE • Delete the above mid-string to form this new stri ng: IT'S EASY TO CHANGE • Add these words to the end of the new string: ANYTHING YOU WANT
• Print the newly formed string: IT'S EASY TO CHANGE ANYTHING YOU WANT
HINT: To form the string IT'S EASY TO CHANGE, you need to get the left portion of the string IT'S EASY TO CHANGE A SENTENCE.
Answer:
10
20
30
50 70
a0
60 80 85 80
100
110
A$ = "CHANGE A SENTENCE + " B$ = "I T 'S EASY TO" C$ = B$ + " " + A$ PR I NT C$ Y = LEN ("A SENTENCE") FOR)-( = 1 TO LEN(C$) IF MID$ (C$d-(,Y)" "A SENTENCE" NEln x END 0$ LEFT$ (C$ ,)-( - 1) E$ = 0$ + "ANYTH I NG YOU WANT" PRINTE$
THEN
90
This program is the basis of a "word processing" program-a popular program thaI cuts down typing expenses.
DO-IT-YOURSELF CHALLENGER PROGRAM Write a program that: • Asks you to input a sentence.
o
Asks you to input (1) a phrase within the sentence to delete and (2) a phrase to replace it. Prints the changed sentence.
This may take a while, but you have everything you need to write it. Our answer's in the back.
69
Learned in Chapter 11
BASIC WORDS
LEN LEFT$
RIGHT$ MID$
Notes
70
CHAPTER 12
A POP QUIZ
By using a word namedINKEY$, you can get the computer to constantly "watch," "time," or "test" what you're typing. Type and run this program: 10 20 30 0 ,A$ =; INKEY$ IFA$<}""GOT050 PR I NT YOU PRESSED GOT 0 1 Ql
II
50
"THE
L "notequal~
I ""
INKEY $ chec ks to see if you're press ing a key. It does th is ina spi it seco nd. At least the first 20 times it checks, you've pressed nothing (" "). l.ine 10 labels decision: the key you press as A$. Then the computer makes a
is an "empty (nothing).
If A$ equals nothing (" "). it prints YOU PRESSED NOTHING goes back to Line 10 to check the keyboard again. If A$ equals something (anything 50 and prints the key. Add this line and run the program: GQ! but"
and
goes to Line
cnr o
No matter how fast you are, the computer what keys you're pressing.
ie 20
30 40
51,\?
=;
Y = RND (4)
PRINT "WHAT T=0 A$;:; INKEY$
"+"
80 70
T=T+1 SOUND 128 ,1 Bill IF T = 15 THEN 200 90 IF A$ = "" THEN '50 100 GOTO 10
200
210 220
180 , 30
"TOO LATE"
works:
lines 10, 20, and 30 have the com pute r pri nt two rand om n umbers a nd ask you for their sum. Line 40 sets T to
o.
T is a timer.
71
Line 50 gives you your first chance to answer the question=-in second.
a split
Line 60 adds 1 to It the timer. I now equals 1. The next time the computer gets to line 60 it again adds 1 to the ti me r to make I equa I 2. Each ti me th e computer runs Line 60 it adds 1 to I. Line 70's there just to make you nervous. Line 80 tells the computer you have 15 chances to answer. Once I equals 15, time's up. The computer insults you with Lines 200, 210, and 220. Line 90 says if you have n' t answered yet the com puler shou Id go back and give you another chance. The computer gets to Line 100 only if you do answer. Line 100 sends it back for another problem. How can you get the computer to give you three times as much time to answer each question?
IF T = 45 THEN 200
II
=" X + Y
120
130 140
If you run th is program (and answer on time), you'll get thi s error message:
?TM ERROR IN 100
That's because you can't make a string (A$) equal to a number (X + Y). You somehow must change A$ to a number. 72
VAl(A$) converts A$ into its numeric value. If A$ equals the string "5," for example, VAL(A$) equals the number 5. If VAL(A$) equals the string "C,iI VAUA$) equals the number o. ("C" has no numeric value.)
To make the program more challenging, change these lines: 10 X = RND(48) +4 20 Y = RND(48) +4 80 . B$ = B$ + A$ 100 IF 1,IAL(B$) = X + Y THEN 130 Then add these lines: 45 85 B$ =1.1 " IF LEN (B$)
<>
2 THEN 50
CLS INPUT "PRESS <ENTER) WHEN READY TO TYPE THIS PHRASE"; E$ PRINT "NOW IS THE TIME FOR ALL GOOD MEN" T=1 A$ = INKEY$ IF A$ = "" THEN 100 PRINTA$ j B$ = B$ + A$ IF LEN(B$) = 32 THEN 120 T=T+1 cern 50 8 = T/74 M = 8/80 R = 81M PRINT PRINT "YOU TYPEDAT--"R"--WDS/MIN"
150 180
73
Line 40 sets T, the timer, to 1. line 50 gives you your first chance to type a key (A$). If you're not fast enough, line 60 sends the program to Line 100 and adds 1 to the timer. line 70 prints the key you typed. line 80 forms a string named B$. Each time you type a key (A$)' the program adds this to B$. For example, if the first key you type is "N," then:
A$ == "N" B$ = B$ + A$
and
B$ = ""
B$
+ "N"
"N"
and
then:
and
B$
<:
"NO"
+ "W."
We coul" have made this calculation in one line by us" ing parentheses:
120 R=8/( 60) (T/711)/
B$ = "NOW" When the length of B$ is 32 (the length of NOW IS THE TIME FOR ALL GOO D MEN), the program assurnes you've fi n ished typ i ng the ph rase and goes to line 120 to compute your words per minute. Lines 120, 130, and 140 compute your typing speed. They divide T by 74 (to get the seconds), S by 60 (to get the minutes). They then divide the eight words by M to get the words per minute.
How
progrsm-:«
test!
Learned in Chapter 12
BASIC WORDS
INKEY$ VAL
Notes
74
CHAPTER 13
MORE BASICS
Before you're finished with the "basics," you need to know a few more words.
A;=
A"
1
A+ 1
30
40
STOP
A '" A .. 2
50
G0
STOP GOTO 20
Thecomputer starts runni ng the program. When it gets to Line 30, it prints:
BREAK OK IN 30
You now can type a command line to see what's happe ning. Far exa mple, type: PR I NT A (ENTER)
The computer continues the program. When it gets to Line SO, it prints:
BREAK IN 50
Type: PR INT
A (EN.TEAJ
Type CONT again, and the computer breaks again at Line 30.lf you have it again print A, it pri nts 5-the val ue of A at line 30 the second time through the program.
Inserting STOP lines in your program helps you figure out why it's not working the way you expect. When you fix the program, take the STOP lines out. 75
PR I NT MEM (ENTER) The computer prints how much storage space remains in the computer's memory. When you're typing j!- long program, you will want to have the computer PRINT MEM from time to time to make sure you're not running out of memory.
INPUT "TYPE 1121 OR 3" j N ON N GoSUB 100 I 200 I 300 cnr o 1 e PRINT "YOU TYPED 1" RETURN PRINT "YOU TYPED 2" RETURN PRINT "YOU TYPED 3" RETURN
20 22
IF N '" 1 THEN GOSUS 100 IF N = 2 THEN GOSUB 200 IF N = 3 THEN COSUB 300 this case N.
casus.
If N is 3, the com puler goes to the subrou Ii ne starti ng at the th ird line What if N is 4? Since there's no fourth line number, the computer simply goes to the next line in the program. Here is a program that uses ON ... COSUB:
1 TO 800:
NEXT P =RND ( 1(0) : Y = RND ( 1(0) (1) ADDITION" (2) SUBTRACTION" "(3) MULTIPLICATIDN" "(ll) DIVISION" "WHICHEXERCISE(1-Ll)"j R
76
crrr n
ON R GoSUB 5
1000,2000,
H "+" PRINT
3000,
Y
11000
1030
00fl?
"CORRECT·"
ELSE
PRINT "WHAT IS" H "_" INPUT A IF A" }{_y THEN P·RINT PRINT "WRONG" RETU.RN PRINT INPUT IF A '" PR I NT RETURN
Y "CORRECT" ELSE
I;LSE
1:1000 PRINT
4030
"WHAT IS" X "/" I N PUT A IF A " )UY THEN PRINT PR I NT "WRONG" RETURN
ELSE
Notice the word ELSEin Lines 1020,2020,3020, and 4020. You can use ELSE you want the computer to do something special when the condition if is not true. In Line 1020, if your answer-A-equals X + Y, then the computer prints CORRECT or else it prints WRONG. You may use ON ... GOIO in a similar way asON ... GOSUB. Theonly difference is that ON GOIO sends the computer to another line number rather than to a subroutine.
10 20 30
1I~. 50
80
G5 70
CLS PRINT @ PRINT @ PRINT @ PRINT @ INPUT.A CLS DNA GDrD PR INT END PRINT END PR I NT END
TO PLAY"
1000 • 2000.
@
1000 1010
~~ Z0Q10
230. "CRAZY
238. "500
"
2010
3000
3010
235. "HEA.RTS
77
20
30
1I0
50
"DO YOU HAVE--" A DEGREE IN PROGRAMM I NG" ; 0$ "E){PERIENCE IN PROGRAMMING" j E$ '" "YES" AND E$ = "YES" THEN PRINT "YOU THE JOB" ELSE PRINT "SORRY I WE CAN'T YOU" 10
Run the program. You may answer the questions this way:
DO YOU HAVE-A DEGREE IN PROGRAMM I NG? NO EXPERIENCE IN PROGRAMMING? YES SORRY I WE CAN'T HIRE YOU
Now, assume the requirements change so that "or" becomes "and." The job now requires: A degree in programming OR Experience in programming To make this change in the program, type: 1I0
IF 0$ = "YES" OR E$ = "YES" "YOU 'I)E GOT THE JOB" ELSE CAN'T HIRE YOU" THEN PRINT PRINT "SORRY,
WE
Run the program and see what a difference AND and OR makes: DO YOU HAI,IE-A DEGREE IN PROGRAMMING? NO EXPER I ENCE IN PROGRAMM I NG? YES YOU HA~)E THE JOB
78
More Arithmetic
Thesewords can save many program lines:
SGN
SGN tells you whether a number is positive, negative, or zero:
10 20 30 INPUT "TYPE IF SGN (X) IF SGN(}() IF SGN(}{) GOTo 10 A NUMBER"; X 1 THEN PR I NT "POS IT IlJE" 0 THEN PRINT "ZERO" -1 THEN PRINT "NEGATIVE"
all!
50
= = =
15
ABS
-30
-.012
ABStells you the absol ute val ue of a number (the magnitude of the number without respect to its sign). Type:
10 20 30 INPUT "TYPE A NUMBER"; PRINT "ABSOLUTE VALUE GOTD 10 N IS" ABS(N)
Runthe program inputting the same numbers as the ones above. STR$ STR$converts a number to a string. Example:
10 20 INPUT" TYPE A NUMBER" j N A$ = STR$(N) PRINT A$ + " IS NOW A STRING"
30
Notice
the OV (overflow)
Exponents
Typeand run this program to see how the computer deals with very large numbers:
10 20 30 X 1 PRINT}( )-(=X*10 GOTo 20
error at the end. The computer can't handle numbers larger than 1E + 38 or smaller than -I E + 38. (/t rounds off numbers around 1£-38 and -I £-38 to 0.)
a0
The computer prints very large or very small numbers in "exponential notation." "One billion" (1 ,000,000,000l, for example, becomes 1 E + 09, which means "the number 1 followed by nine zeros."
J-;r 1 times
is
technically
1.109, which
10 to the ninth
power:
1-10*10*10. 70.10.10.10.10_10
Ifan answer comes o.ut "SE-06," you must.shift ~hedeci mal point, which ( In our BASIC, that's 51101101 comes after the 5, SIX places to the left, inserting zeroes as necessary. fl 10110110110 Technically, this means 5*10-6, or 5 millionths (,000005). ---...... Exponential notation is simple once you get used to it. You' I I find it an easy way to keep track of very large or very small numbers without losing the decimal point.
79
Congratulations, Programmer!
You've now learned the "basics" and can no doubt write some decent programs. The next section will help you add excitement to your programs with graphics and music.
Learned in Chapter 13
BASIC WORDS
STOP CONT SGN
BASIC SYMBOLS
AND OR
BASIC CONCEPT
Exponential notation
ABS
MEM
STR$
Notes
80
SECTION II
And you'll also be amazed at how quickly and easily you can do thisl So
turn the page and we'll get right to the paint
CHAPTER 14
10 20 30
1I0
Now run the program. The screen should be buff, and if you look carefully, you can see a small orange dot in the upper left corner. That dot was put there by the PSET (poi nt set) in Line 30.
'
Don't worry about any of the new words. PMODE and SCREEN, for imtance, determine the degree of detail and the range of color: They are covered in later chapters .
...... ...
'
. ...
P$ET lets you set a dot anywhere on the screen. It has th is format: PSET (h,v,c) sets a poi nt on the current graphics screen h is the horizontal position (0 to 255).
v is the vertical position (0 to 191). c is the color (0 to 8). If you omit c, BASIC uses the current foregrou nd color.
Eventhough you can't see it, the computer has divided your screen into a grid of nearly 50,000 dots-256 across and 192 down-so that you can put a dot precisely where you want it Simply look up the dot's position in the Graphics Screen Worksheet in the back of this manual. look at Line 30 again and see how PSETspecifies the dot's position (10 over and 20 down): -- 30 PSET (112).20,8)
You'il see these "syntax blocks" throughout this section. They'll help you understand (he "parameters" you can use with graphic statements
85
Here' 5 the statement you wo uId use to set an or all ge dot in the center of the screen: PSET (128,86,8)
Now add a program line that sets an orange dot in the lower right corner (255 over and 191 down). Is th is the I ine you used? 35
Very Important Note! The Color Computer can pro" duce 9 colors: black, green, yellow, blue, red, buff, cyan, magenta, and orange. The actual shade yeu get, though, depends on your T.v.-not the computer: We suggest yeu perform the color adjustment test program in your introduction manual before running these programs.
PSET
Now Jist the program. It should look like this: 5 PMODE 1,1
10 pelS
20 SCREEN 1.1
30 PSET
35 PSET (2550181.8) 1I0 GOTD 1I0 You're off to a great start ...
(10.20.8)
Code
Color
Black Green Yellow Blue Red Buff Cyan Magenta Orange
o
The 8 gives the color (orange.!. Later, we'll di5CU.SS how to change the color: For now, simply use orange.
-- - 1
-
--
2 3 4 5 6 7 8
If you want to try changing the dots' colors, use buff (5), cyan (6), or magenta (7). Then change the color back to orange (8) before proceed-
86
Any guesses how to turn off a dot? Here's a hint: It's easy and it has to do with color.
You see, you don'! really turn off the dot, you simply change its color
so
that it blends into the background. You do this with a new statement:
PRESET (point reset). PRESET"knows" you wan! to use the background color, so you don't need to give the color.
PRESET(h,v) resets a point on the current graphics screen
------------------------~
DO-IT-YOURSELF PROGRAM 14-1 Get to know the dot positions on your TV screen by using your Graphics Screen Worksheet. Select several points on the worksheet, identify them in terms of their (X,Y) coordinates, and display them on the screen using the program we used to get you started. Don't change any program lines except those that contain PSET(h,v,c). DO-IT-YOURSELF PROGRAM 14-2 Do you remember the RND (random) function from Section I? If not, review it; then write a short program that fills the screen with random dots of random colors.
L
5
35
PPOINT (hI v) tells what color a point is on the current graphics screen
PMoDE 3 t1 PClS
PSET (X.Y,C) IF PPDINT (5.5)=8 THEN GoTD 105 70 GOTo 30 105 CLS
110 PRINT @ 100. "POSITION IS NOW ORANGE" (5.5)
50 80
40
87
The computer f lis a lOX 10 "square" (in the upper left corner of the screen) with random colored dots. When the dot in Position (5,5) is filled with an orange dot (Code 8), the computer displays the message POSITION (5,5) IS NOW ORANGE.
Learned in Chapter 14
BASIC WORDS CONCEPTS
Setting poi Ills Resetting points Changing colors Finding a point's color
Notes
88
CHAPTER 15
Then delete Line 35 by typing: 35 {ENTER) Your program should now read: 5 PMDDE 1,1 10 PClS 20 SCREEN 1,1 30 lHIE (0,0)-(255,181> 40 GOTD 40
,PSET
Now run the program. The screen should display an orange line that runs from the upper left to the lower right on a buff background. How about ch angi ng th e di recti 0 n of til eli ne so th at it ru ns from the lower left to the upper right?
89
You've probab Iy aIready f gu red th is one out, bUI-J new Line 30: 30 LINE (01181)-(255,0) ,PSET
USI incase-he
re's the
Using the Grap;:;cs Worksheet, plot the points used in creating the intersecting lines in the "Lines" program.
5cre",;-1
I I
Reinsert the original Line 30 that drew the first line. (First, renumber it as Line 25.) Then run the program. Does your screen display 2 orange Iinesintersecti ng in the center? In fact, you can put as many lines on the screen as you want-once you learn the format. Here it is:
LINE (hi,1I1)-(h2,v2),a-h gra ph ics screen
(hl,vJ) (h2,v2)
is the line's start point. is the line's end point. a is either PSET (set) or PRESET (reset) b is either B (box) or BF (box filled). This is optional. Note: You may omit the start point as discussed below.
Just as in the old dot-to-dot days, you may often want to draw a line that begins at the Iast lin e' s end po int. W henever this is the case. you may am it the start point. The computer automatically starts at either the end point set by the latest LINE statement or-if you haven't yet used LINE in the program-at (128,96). Here is an example: 30 35 LINE LINE (0,0)-(255tl81) -(191 d~) ,PSET ,PSET
Line 20 draws a line from (0,0) to (255,191 J. Line 30 then draws another line, thi s one from (255,191) to point (191 ,0). Regardless of whether or not you include the start point, you must precede the end point with a hyphen H.
Take another look at the program lines that created the intersecting lines: 30 LINE 35 LINE (0,12)-(255.181) ,PSET (!llt181)-(255,0),PSET
From your experience with turning on and off dots in Chapter 14, do you haveany idea what the PSETparameter is doing and what would happen if you change it to PRESET? Try it and see. Change the P$ET in Line 25 to PRESET and run the program again: 30 LINE (!ll tlllJ-(255 t181l ,PRESET
If you guessed that the orange I ine that ran from the upper left to the lower right would "disappear," you were right. Now replace PSETin Line 30 with PRESET.The screen went blank, right? The reason is the way PSET and PRESETwork in a LINE statement: PSETsets the line in the pre-specified foreground color. PRESET sets the line back to the pre-specified color so that you can't see it. backgrou nd
The color specification is elsewhere in the program, not in the LINE command, sci we'll come back to it later. For now, iust concentrate on l_I:INE.
Note: The PSET and PRESET parameters in a LINE statement are not the same as the PSET and PRESET statements discussed in Chapter 14. They do not specify a dot or a color code. They merely specify that the line be set to the foreground or the background color. Before proceeding, change the PRESETparameters in Lines 25 and 30 back to PSET.
With Extended Color BASIC, you separate program line for each opposing corners of the box and run the program, your computer
can make a box without having to write a side. All you have to do is specify two add ,B to the statement. Then, when you creates a box instead of a line. program back into service.
To illustrate this, call your "Lines" 5 PMODE1 t1 10 PClS 20 SCREEN 1 t 1 25 LINE (0,12)-(255.181) 30 LINE (01181)-(255,!ll) Ll0 GoTo LI!ll
,PSET ,PSET
As is, the program creates 2 orange Iines that intersect in the center of the screen. Delete line 30 and add the suffix ,B to Line 25. Now see what happens when you run the program .. 25 LINE (!ll,!ll)-(255t181),PSET,B
15- 1
Write a program that creates a box with a pair of lines intersecting in the center. We'll tell you why these are the only avai lable colors when we discuss PMODE and SCREEN 'in the next 2 chapters.
Fill It Up
We're almost at the end of the LINE, so let's try to finish. If you refer to the format of LINE, you can seeyou have the option of adding F to the optional suffix, B. F lets you "f II" the box with the foreground color. Try it. Change Li ne 25 as follows: 25 LINE '(!2lI!21)-'(255,181),PSET,BF
How about that! You should have a big orange box (256 x 192) on a buff backgrou nd. .
specifyit each time you put something on the screen. With the COLOR
feature, you don't have to. Within certain limits, the graphics feature COLOR lets you set the foreground/background colors. (See "PMODE" and "SCREEN" later in this book.) Here is its format:
COLOR foreground,background sets the foreground and background color on the current graphics screen foreground is the code (0 to 8) for the foreground color background is the code (0 to 8) for the background color.
Not.e: As stated in Chapter 14, the on Iy colors avaiIable with you r current program are buff (5), cyan (6), magenta (7), and orange
(8).
When you don't specify the foreground and background colors, your computer automatically chooses the highest-numbered available color code for the foreground color and the lowest-numbered available color code for the background color. That's why the crossing lines in the "Lines" program are orange (8) on a buff background (5). To see COLOR in action, calion 5 MODE 1 Ii ,PSET "Lines" again:
10 pelS
20 SCREEN 1 d 25 LINE (0,0)-(2551181) 92
30 Ll0
LINE GOTD
(0t191l-(255t0ltPSET Ll0
If you used your Color Computer to draw an airplane and used COLOR to give it the right cotot, would you have flying colors?
COLOR
5t7
Now run the program. What do you think of buff lines crossing on a magenta background? Do you want to see what the colors look I ike when reversed? If so, retype or
In the next chapter, you'll learn how to make even more colors available.
DO-IT-YOURSELF PROGRAM 15-2 Ready to try your own "Lines" program? Can you build a house? Start wi th Li nes 5, 10, and 20 of the' 'L ines" program and take it from there. Be sure to add: A front door, of course. At least one wi ndow. (Don't forget to turn the lights on or off.) A chimney. (You won't need a chimneysweep, not yet anyway!) The overall design is up to you (Cape Cod, Ranch, or whatever), but we've included a sample house (good view, no pets) program in the back of the book. Don't worry about doorknobs; we'll add those later. Be sure to save this program on cassette, since you'll be needing it later. (You'll find it much easierto draw the house if you plotits points on a Graphics Screen Worksheet.)
DO-IT-YOURSELF PROGRAM 15-3 This should be a real challenge for you. As you know, a straight line is the shortest distance between two points. Well, put a few extra miles between our two points. Use LINE to draw a crooked line. To get started, use Lines 5, 10, and 20 from the "Lines" program.
COLOR is not an action statement; it must precede an action statement (such as peLS or LINE) before the foreground and background colors are actually changed.
Learned
BASIC WORDS
LINE
In
Chapter 15
CONCEPTS Drawing a line Erasing a line Drawing a box Fi 1.1 ing in a box Changing foreground and background colors
COLOR
93
Notes
94
CHAPTER 16
,PSET ,PSET
SCREEN tells the computer to display a screen image on your TV. What
kind of screen it displays depends on the instructions you give it: First, you te II the com puter whether to use the TV screen for text (such as letters or numbers) or graphics (such as I ines and circles). Second, you tell the computer what "color set" to use.
95
SCREEN type, color set displays the current graphics or text screen
type is 0 (text screen) or 1 (graphics screen) color set is 0 or 1
Note: If type or color set is any positive number greater than 1, your computer uses 1.
SCREEN
QI ,QI
Then run the program. Does your computer "hang up"? {press (BREA-Rl to regain control.) Ac!ua!l y, th e computer ran" Lin es," just as before, Th is ti me, though ,it did not show you the gra ph ics screen. You asked to see the text screen instead. Now change Line 20 to:
2Q1 SCREEN 1 ,QI
Notice that you have the graphics screen again, but this time the color set has been changed.
'
At first glance, it appe rs that you have only 2 color choices-O and 1. Actually, though, you're choosing from a much greater variety: You're switching color sets, not individual colors. Color Set 0 Color Set 1 GreenIYellow/Blue/Red Bu ffle yan/Mage nta/Orange
DO-IT - YOU RSELF PROG RAM 16- 1 Do you understand SCREEN? If you do, write a program that switches from text screen to graph i cs screen. You might wa n t to put a loop in the program so that it changes the color set after it loops through the program. This way you can see all the SCREEN features at work.
LINE
,PSET ,PSET
1I0
GOTO
1IQ1
Look at line 10. It contains the pelS statement. This statement simply clears the graphics screen. (It serves the same function for the graphics screen as CLS does for the text screen.)
96
The "Lines" program doesn't make use of PClS's cotor option. Therefore, the computer uses the current background color, buff. Retype line 10: 10 pelS G Your screen now displays orange lines on a cyan
Learned in Chapter 16
BASIC WORDS SCREEN PClS CONCEPTS
Displaying the current screen Clearing the graphics screen
Notes
97
CHAPTER 17
MINDING
YOUR PMODES
What lets you produce exciting graphics is the massive size of graphics memory. To get a perspective on this, contrast graphics and text memory: Text memory has 512 memory locations: graphics memory has up to 12,288!
You can use the power of graphics memory in three ways: To produce graphics with very high resolution (fine detail). To produce graphics with many colors. To produce fast-changing, "animated" graphics screens in memory at once. graphics by retaining many
How much you can use of each of these features depends on how you "set" graphics memory. The more you use of one feature-such as retaining many screens in memory-the less you can use of the other features (high resolution and colors). PMODE-the unknown statement in the "Lines" program-is what sets the features you want to use. PMODE lets you set 5 "modes," shown in Table 17-1. Each mode, of course, has its own trade-off of features.
4 3 2 1 0
high
medium medium low low
Colors 2 4 2 4 2
Screens
2 2
4 4
8
98