The Secret Guide to Computers
To program the computer, you must use a language that the computer understands. Most computers understand a language called BASIC, which is a small part of English.
BASIC was invented by two Dartmouth College professors (John Kemeny and Tom Kurtz) in 1964. Later they improved it. Now BASIC consists of words such as PRINT, INPUT, IF, and THEN.
I’ll explain how to program the computer by using those BASIC words.
Different computers speak different dialects of BASIC. The most popular dialect was invented in 1975 by a 19-year-old kid, Bill Gates. Since he developed software for microcomputers, he called himself Microsoft and called his BASIC dialect Microsoft BASIC.
Since Microsoft BASIC is so wonderful, all the popular computer companies paid him to make their computers understand Microsoft BASIC. That’s right: IBM, Apple, Commodore, Tandy, Atari, Texas Instruments, and hundreds of other computer companies all had to pay off Bill.
Microsoft BASIC has become so popular that Bill had to hire hundreds of employees to help him fill all the orders. Microsoft Incorporated has become a multi-billion-dollar company, and Bill has become a famous billionaire, the wealthiest person in America.
Over the years, Bill gradually improved Microsoft BASIC. Some computers use old versions of Microsoft BASIC; other computers use his latest improvements.
Now the most popular version of Microsoft BASIC is QBASIC. It’s a simplification of another version of Microsoft BASIC, called Quick BASIC.
QBASIC is popular because it’s good and because most people get it at no charge: free!
QBASIC is included, free, when you buy MS-DOS (versions 5, 6, 6.2, 6.21, or 6.22) or IBM PC-DOS 5 (which is similar to MS-DOS 5). QBASIC does not come with IBM PC-DOS 6.1, 6.3, or 7.
If your computer has Windows 95 or 98 or 98SE or Me and you got a CD-ROM disk containing Windows, that CD-ROM disk includes QBASIC; but QBASIC does not come with Windows XP.
This chapter explains how to use QBASIC. (Later chapters explains Visual BASIC and other BASICs.) QBASIC’s commands are explained on these pages:
QBASIC’s functions are explained on these pages:
QBASIC’s F keys are explained on these pages:
F2 SUB show a different SUB 446
F4 View switch between blue & black screens 378
F5 Continue continue running the program 412
SHIFT F5Run run the entire program 377
F6 Immediate move to & from immediate window 412
F7 Above run the program down to here 412
F8 Step run one more line of the program 412
F9 Breakpoint create or destroy a red breakpoint 412
To start using QBASIC, turn on the computer without any floppy in drive A.
Make sure the screen shows this standard C prompt:
(To make the screen show it, follow the instructions on page 120 of the MS-DOS chapter. Those instructions work even if you’re using Windows 3 or 3.1 or 3.11 or 95 or 98 or 98 SE or Me.)
To start using QBASIC, type “qbasic” after the C prompt, so your screen looks like this:
When you press the ENTER key at the end of that line, see what happens!
If you’re lucky, the screen will turn blue and the computer will say:
If you’re unlucky, the computer will gripe by saying “Bad command or file name”. To find out why the computer is griping, type “ver” after the C prompt. Then the computer will tell you which version of DOS it’s using. To run QBASIC, you must buy DOS version 5, 6, 6.2, or 6.22 (since QBASIC is not included with DOS versions 6.1 or 6.3 or 7), or else buy the CD-ROM version of modern Windows (95 or 98 or 98 SE or Me) and copy QBASIC from that CD-ROM to your hard disk as follows:
Insert the CD-ROM disk that Windows came on. (Some computer makers call it the “Windows Companion Disk” or the “Recovery CD”.)
If that disk’s window is on the screen, close the window by clicking its X box. Make sure the computer says “C:\>” (by following the instructions on page 120).
If you’re using Windows 98 or 98SE or Me, type this:
If you’re using Windows 95, type this:
After the asterisk, make sure you put a space. If your CD-ROM drive is called “drive E” instead of “drive D”, type “e:” instead of “d:”.
Then try again to type “qbasic” after the C prompt.
After the computer says “Welcome to MS-DOS QBASIC”, press the Esc key (which is at the keyboard’s top left corner).
Type your program
Now you’re ready to type your first program!
For example, type this program:
Here’s how. Type CLS, then press the ENTER key at the end of that line. Type PRINT 4 + 2 (and remember to hold down the SHIFT key to type the symbol +), then press the ENTER key at the end of that line.
Notice that you must press the ENTER key at the end of each line.
A program is a list of commands that you want the computer to obey. The sample program you typed contains two commands. The first command (CLS) tells the computer to CLear the Screen, so the computer will erase the screen and the screen will become blank: entirely black! The next command (PRINT 4 + 2) tells the computer to do some math: it tells the computer to compute 4 + 2, get the answer (6), and print the answer on the screen.
Run your program
To make the computer obey the program you wrote, do this: while holding down the SHIFT key, tap the F5 key. That tells the computer to run the program: the computer will run through the program and obey all the commands in it.
The computer will begin by obeying the CLS command. The computer will clear the screen and make the screen become all blank, all black.
Then the computer will obey the PRINT 4 + 2 command. The computer will print this answer onto your screen:
Congratulations! You’ve written your first program! You’ve programmed the computer to compute the answer to 4 + 2! You’ve become a programmer! Now you can put on your résumé: “programmer!”
When you finish admiring the computer’s answer, press the F4 key. That makes the screen change: instead of showing the computer’s answer, the screen will turn blue and show your program again:
If you’d like to peek at the answer again (which is 6 on a black screen), press the F4 key again. When you finish peeking at the answer, press the F4 key again to view the program again (CLS and PRINT 4 + 2 on a blue screen).
So here are the rules:
To run the program (and put the answer on a black screen), press SHIFT with F5.
To view the blue screen again — or switch back to the black screen again — press F4.
While typing the program, you don’t need to capitalize computer words such as CLS and PRINT: the computer will capitalize them automatically when you press ENTER at the end of the line.
While typing that program, put a blank space after the word PRINT to separate the “PRINT” from the 4. But you don’t need to put spaces next to the + sign, since the computer will automatically insert those spaces when you press ENTER at the end of the line.
Instead of typing “PRINT” or “print”, you can type just a question mark. When you press the ENTER key at the end of the line, the computer will replace the question mark by the word PRINT, and the computer will put a blank space after it.
So instead of typing PRINT 4 + 2, you can type just this:
Think of the question mark as standing for this word:
If you want to ask the computer “What’s 4+2”, type this:
When you run the program, the computer will print the answer, 6.
The program’s top line (CLS) tells the computer to erase the screen before printing the answer (6).
If you forget to make the program’s top line say CLS, the computer will forget to erase the screen. The computer will still print the answer (6), but that answer will appear underneath a transcript of previous chit-chat that occurred between you and the computer. That transcript is distracting and confusing. CLS erases it.
Edit your program
After you’ve typed your program, try typing another one. For example, create a program that makes the computer print the answer to 79 + 2. To do that, make this program appear on the screen:
To make that program appear, just edit the program you typed previously (which said PRINT 4 + 2). To edit, use the arrow keys to move to the character you want to change (which was the 4), delete that character (4) by pressing the DELETE key, then type the characters you want instead (79).
While editing, use these tricks.…
To delete a character:
move the cursor (blinking underline) to that character, then press the DELETE key.
To delete SEVERAL characters:
move to the first character you want to delete, then hold down the DELETE key awhile.
To delete AN ENTIRE LINE:
move to that line; then while holding down Ctrl key, tap the Y key.
To INSERT A NEW LINE between two lines:
move to the beginning of the lower line; then while holding down Ctrl key, tap the N key.
If you’ve edited the program successfully, the screen shows just the new program —
and you don’t see the old program anymore.
When you’ve finished editing the program, run it (by pressing SHIFT with F5 again). Then the computer will print the answer:
Fix your errors
What happens if you misspell a computer word, such as CLS or PRINT? For example, what happens if you accidentally say PRIMPT instead of PRINT?
When you run the program (by pressing SHIFT with F5), the computer tries to run each line of your program. If the computer comes to a misspelled computer word (such as PRIMPT), the computer highlights your misspelling (by showing it in blue letters against a white background) and says:
Press the ENTER key, then fix your error, then try again to run the program (by pressing SHIFT with F5 key again).
This command makes the computer add 4 + 2:
Put that command into a program (whose top line should be CLS). When you run the program (by pressing SHIFT with F5), the computer will print the answer:
If you want to subtract 3 from 7, type this command instead:
(When typing the minus sign, do not press the SHIFT key.) The computer will print:
You can use decimal points and negative numbers. For example, if you type this —
the computer will print:
Multiplication To multiply, use an asterisk. So to multiply 2 by 6, type this:
The computer will print:
Division To divide, use a slash. So to divide 8 by 4, type this:
The computer will print:
Avoid commas Do not put commas in big numbers. To write four million, do not write 4,000,000; instead, write 4000000.
The symbol # If you type a long number (such as 7000000000 or 273.85429), the computer might automatically put the symbol # afterwards. That’s the computer’s way of reminding itself that the number is long and must be treated extra carefully!
Use decimals for big answers The computer sometimes has difficulty handling answers bigger than 32 thousand. To avoid difficulty, put a decimal point in any problem whose answer might be bigger than 32 thousand.
For example, suppose you want the computer to multiply 200 by 300. Since the answer to that problem is 60 thousand, which is bigger than 32 thousand, you should put a decimal point in that problem. But suppose you forget to insert a decimal point, and you say just this:
the computer will complain by saying:
When the computer says “Overflow”, reply by pressing the ENTER key, then fix your program by inserting a decimal point, like this —
or like this —
When you finish typing that line (and press ENTER afterwards), the computer will do something strange: it will turn the “.0” or “.” into an exclamation point, so the line looks like this:
When you run the program, the computer will print the right answer:
Notice that if you type a decimal point at the end of a number, the computer usually puts an exclamation point (!) at the end of the number. If the number is long, the computer puts a number sign (#) instead of an exclamation point.
E notation If the computer’s answer is huge (more than a million) or tiny (less than .01), the computer might print an E in the answer. The E means “move the decimal point”.
For example, suppose the computer says the answer to a problem is:
The E means, “move the decimal point”. The plus sign means, “towards the right”. Altogether, the E+12 means, “move the decimal point towards the right, 12 places.” So look at 8.516743, and move the decimal point towards the right, 12 places; you get 8516743000000.
So when the computer says the answer is 8.516743E+12, the computer really means the answer is 8516743000000, approximately. The exact answer might be 8516743000000.2 or 8516743000000.79 or some similar number, but the computer prints just an approximation.
Suppose your computer says the answer to a problem is:
After the E, the minus sign means, “towards the left”. So look at 9.23, and move the decimal point towards the left, 6 places. You get:
So when the computer says the answer is 9.23E-06, the computer really means the answer is:
You’ll see E notation rarely: the computer uses it just if an answer is huge (many millions) or tiny (tinier than .01). But when the computer does use E notation, remember to move the decimal point!
D notation If the answer’s a long number, the computer usually prints a D instead of an E. Like the E, the D means “move the decimal point”.
The highest number The highest number the computer can handle well is about 1E38, which is 1 followed by 38 zeros, like this:
If you try to go much higher, the computer will either gripe (by saying “Overflow”) or use D notation (which goes up to about 1D308).
The tiniest decimal The tiniest decimal the computer can handle easily is about 1E-38, which is a decimal point followed by 38 digits, 37 of which are zeros, like this:
If you try to go much tinier, the computer will either say 0 or use D notation (which goes down to about 1D-323).
Order of operations What does “2 plus 3 times 4” mean? The answer depends on who you ask.
To a clerk, it means “start with 2 plus 3, then multiply by 4”; that makes 5 times 4, which is 20. But to a scientist, “2 plus 3 times 4” means something different: it means “2 plus three fours”, which is 2 + 4 + 4 + 4, which is 14.
Since computers were invented by scientists, computers think like scientists. If you type —
the computer will think you mean “2 plus three fours”, so it will do 2 + 4 + 4 + 4 and print this answer:
The computer will not print the clerk’s answer, which is 20. So if you’re a clerk, tough luck!
Scientists and computers follow this rule: do multiplication and division before addition and subtraction. So if you type —
the computer begins by hunting for multiplication and division. When it finds the multiplication sign between the 3 and the 4, it multiplies 3 by 4 and gets 12, like this:
So the problem becomes 2 + 12, which is 14, which the computer prints.
For another example, suppose you type:
The computer begins by doing all the multiplications and divisions. So it does 2 * 3 (which is 6) and does 72 / 9 * 5 (which is 8 * 5, which is 40), like this:
So the problem becomes 10 - 6 + 40, which is 44, which is the answer the computer prints.
You can use parentheses the same way as in algebra. For example, if you type —
the computer will compute 5 - 2 and print:
You can put parentheses inside parentheses. If you type —
the computer will compute 10 - (5 - 2), which is 10 - 3, and will print:
Let’s make the computer fall in love. Let’s make it say, “I love you”.
Type this program:
Here’s how to type the second line:
Begin by typing the word PRINT. Then type a blank space (by pressing the SPACE bar). Then type a quotation mark, but be careful: to type the quotation mark, you must hold down the SHIFT key. Then type these words: I love you. Then type another quotation mark. At the end of that line, press the ENTER key.
When you run the program (by pressing SHIFT with F5), the computer will obey your command; it will print:
You can change the computer’s personality. For example, if you give this command —
the computer will reply:
Notice that to make the computer print a message, you must put the message between quotation marks. The quotation marks make the computer copy the message without worrying about what the message means. For example, if you misspell “I love you”, and type —
the computer will still copy the message (without worrying about what it means); the computer will print:
Faster typing Instead of typing —
you can type just this:
At the end of that line, when you press the ENTER key, the computer will automatically do three things:
The computer will change the question mark to the word PRINT. The computer will put a blank space after PRINT (and before the quotation mark). The computer will put a quotation mark at the end of the line (to match the other quotation mark).
Jargon The word “joy” consists of 3 characters: j and o and y. Programmers say that the word “joy” is a string of 3 characters.
A string is any collection of characters, such as “joy” or “I love you” or “aieee luf ya” or “76 trombones” or “GO AWAY!!!” or “xypw exr///746”. The computer will print whatever string you wish, but remember to put the string in quotation marks.
Strings versus numbers
The computer can handle two types of expressions: strings
and numbers. Put strings (such as “joy” and “I love you”)
in quotation marks. Numbers (such as
Accidents Suppose you accidentally put the number 2 + 2 in quotation marks, like this:
The quotation marks make the computer think “2 + 2” is a string instead of a number. Since the computer thinks “2 + 2” is a string, it copies the string without analyzing what it means; the computer will print:
It will not print 4.
Suppose you want the computer to print the word “love” but you accidentally forget to put the string “love” in quotation marks, and type this instead:
Since you forgot the quotation marks, the computer thinks love is a number instead of a string but doesn’t know which number, since the computer doesn’t know the meaning of love. Whenever the computer is confused, it either gripes at you or prints a zero. In this particular example, when you run the program the computer will print a zero, like this:
So if you incorrectly tell the computer to proclaim its love, it will say zero.
Longer programs You can program the computer say it’s madly in love with you!
Let’s make the computer say:
To make the computer say all that, just run this program:
To run that program, type it and then press SHIFT with F5. Try it!
To have even more fun, run this program:
It makes the computer print “I long”, then print the answer to 2+2 (which is 4), then print “U”. So altogether, the computer prints:
Yes, the computer says it longs for you!
Printing can be tricky! Here are the tricks.
Indenting Suppose you want the computer to print this letter:
This program prints it:
In the program, each line contains two quotation marks. To make the computer indent a line, put blank spaces AFTER the first quotation mark.
Blank lines Life consists sometimes of joy, sometimes of sorrow, and sometimes of a numb emptiness. To express those feelings, run this program:
CLS Clear the screen.
PRINT "joy" Print “joy”.
PRINT Print a blank empty line, underneath “joy”.
PRINT "sorrow" Print “sorrow”.
Altogether, the computer will print:
Semicolons Run this program:
The second line, which makes the computer print “fat”, ends with a semicolon. The semicolon makes the computer print the next item on the same line; so the computer will print “her” on the same line, like this:
This program gives you some food for thought:
The program says to print three phrases. Because of the semicolons, the computer tries to print all the phrases onto a single line; but those phrases are too long to all fit on the same line simultaneously! So the computer prints just the first two phrases onto the line and prints the third phrase underneath, like this:
The next program shows what happens to an evil king on a boat:
The computer will print “sin”, and will print “king” on the same line, like this:
Notice that in a PRINT statement, you can type several items (such as “sin” and “king”). You’re supposed to type a semicolon between each pair of items; but if you forget to type a semicolon, the computer will type it for you automatically when you press the ENTER key at the end of the line. The computer will also automatically put a blank space after each semicolon.
Spaces after numbers Try typing this command:
Whenever the computer prints a NUMBER, it prints a blank space afterwards; so the computer will print a blank space after -3, like this:
Spaces before positive numbers This command tells what to put in your coffee:
The computer prints 7 and “do” and “nuts”. Since 7 is a number, the computer prints a blank space after the 7. The computer prints another blank space BEFORE every number that’s positive; so the computer prints another blank space before the 7, like this:
Hey, if you’re feeling cool, maybe this command expresses your feelings:
The computer prints “the temperature is”, then 4 + 25 (which is 29), then “degrees”. Since 29 is a positive number, the computer prints a blank space before and after the 29:
Fix the negative numbers Use this command if you’re even colder:
The computer prints “the temperature is”, then 4 - 25 (which is -21), then “degrees”. Since -21 is a number, the computer prints a space after it; but since -21 is not positive, the computer does not print a space before it. The computer prints:
no space space
Yuk! That looks ugly! It would look prettier if there were a space before the -21. To insert a space, put the space inside quotation marks:
inserted space, before the quotation mark
Then the computer will print:
Multiple calculations By using semicolons, you can make the computer do many calculations at once.
For example, this command makes the computer do 6+2, 6-2, 6*2, and 6/2, all at once:
That makes the computer print the four answers:
The computer prints spaces between the answers, because the computer prints a space after every number (and an additional space before every number that’s positive).
Print on paper If you say LPRINT instead of PRINT, the computer will print on paper instead of on your screen.
For example, if you want the computer to compute 2+2 and print the answer on paper, type this program:
While typing that program, make sure you type “LPRINT”. Although “PRINT” can be abbreviated by typing “?”, “LPRINT” cannot be abbreviated by typing “L?”; you must type the word “LPRINT” in full.
When you run that program (by putting paper into the printer, turning the printer on, and pressing SHIFT with F5), the computer will compute 2 + 2 and print this answer onto paper:
Although the computer prints that answer onto paper, the paper remains stuck in the printer, until you tell the printer to eject the paper. Ejecting the paper is called “doing a form feed”, because it feeds a sheet of paper (a form) through the printer.
To eject the paper manually, press the printer’s form-feed button.
For example, if your printer’s an Epson 5000 (or a similar dot-matrix printer), push the printer’s LF/FF button awhile (because that’s the form-feed button). If your printer’s a Hewlett-Packard Laserjet 2 (or a similar laser printer), press the printer’s ON LINE button (so the ON LINE light turns off), then the FORM FEED button, then the ON LINE button again (so the ON LINE light turns back on).
Instead of ejecting paper manually, you can make the computer eject paper AUTOMATICALLY, by putting this line at the bottom of your program:
That line works for all popular printers.
For example, this program makes the computer figure out the answer to 2+2, print the answer (4) onto paper, and then eject the paper from the printer:
This program prints a poem on paper and then ejects the paper:
If you want to print several copies of that poem onto paper (so you can hand the copies to several friends), run that program several times: each time, say run (by pressing SHIFT with F5) and return to the blue screen (by pressing F4).
If you say PRINT 2 + 2, the computer prints the answer (4) onto the screen. If you say LPRINT 2 + 2, the computer prints 4 onto paper instead. If you want to print the answer onto the screen and also onto paper, say PRINT and also LPRINT, like this:
CLS Clear the screen.
PRINT 2 + 2 Print answer (4) onto screen.
LPRINT 2 + 2 Print answer (4) onto paper.
LPRINT CHR$(12);Eject paper from the printer.
Here’s another way to print on paper:
WIDTH 40 At the top of your program, instead of saying CLS, you can say:
Like the CLS command, it makes the computer erase the screen. But it also makes all characters on the black screen be extra wide. It makes those characters be twice as wide as normal, so just 40 of them fit on a line (instead of 80). The characters look dramatically huge!
Though WIDTH 40 widens the computer’s answers on the black screen, it does not widen characters on paper or on the blue screen (where you type your program).
If you tap the Alt key and then the F key, you’ll see this file menu:
│ New │
│ Open... │
│ Save │
│ Save As... │
│ Print... │
│ Exit │
Here’s how to use it.…
Print If you choose Print from that menu (by pressing the P key) and then press the ENTER key, the computer will copy the program onto paper.
For example, if the screen shows this program —
the printer will print this onto paper:
Then eject the paper manually.
Save If you want the computer to copy the program onto your hard disk, choose Save from the file menu, by pressing the S key.
If you haven’t invented a name for the program yet, the computer will say “File Name” and wait for you to invent a short name. Invent any short name you wish. For example, the name can be JOE or SUE or LOVER or POEM4U.
Pick a name that reminds you of the program’s purpose.
For example, if the program prints a bill to a customer, call the program “BILL”; if the program plays chess, call the program “CHESS”; if it gives a quiz, call it “QUIZ”; if it tutors a human about the elements of sex, call it “SEX”; if it tutors a human about advanced sex, call it “SEX2”.
The name must be short (up to 8 characters long) and should be simple (consisting of just letters and digits).
When you finish typing the name, press the ENTER key. Then the computer copies your program to the hard disk.
For example, if you typed the name “joe”, the computer copies your program to the hard disk and names the program “JOE.BAS”. (Notice that the computer automatically capitalizes the name and puts .BAS afterwards. The .BAS means “written in BASic”.)
Exception: if the name you invented was already used by another program, the computer asks you, “Overwrite?” Press the Y key if you want the new program to replace the old program, so the old program disappears. If you do not want the new program to replace the old program, press N instead of Y, then invent a different name for your new program.
Suppose you’re creating a program that’s so long it takes you several hours to type. You’ll be upset if, after several hours of typing, your town suddenly has a blackout that makes the computer forget what you typed. To protect yourself against such a calamity, choose Save from the file menu every 15 minutes.
Then if your town has a blackout, you’ll lose just a few minutes of work; the rest of your work will have already been saved on the disk. Saving your program every 15 minutes protects you against blackouts and also again “computer malfunction” and any careless errors you might make.
New When you’ve finished inventing and saving a program, here’s how to erase the screen, so you can start writing a different program instead: choose New from the file menu (by pressing the N key).
If you didn’t save the program you worked on, the computer asks, “Save it now?” If you want to save the program you worked on, press the Y key; if you do not want to save the program you worked on, press the N key instead.
Open If you saved a program onto your hard disk, here’s how to use it again: choose Open from the file menu (by pressing the letter O).
The computer shows you an alphabetical list of all BASIC programs on the hard disk. (If the list is too long to fit on the screen, the computer shows you the list’s beginning.)
Then say which program you want, by using one of these methods.…
Method 1:type the name of the program you want (such as “joe”), then press ENTER.
Method 2: press the TAB key, then the down-arrow key; then press the down-arrow key a few more times until the program you want is highlighted; then press the ENTER key.
All lines of that program will appear on the screen.
Exception: if a different program has been on the screen and you didn’t save it, the computer will ask, “Save it now?” If you want to save that program, press the Y key; if you do not want to save that program, press the N key instead.
ESCAPE key If you change your mind and wish you hadn’t requested the file menu, press the ESCAPE key (which says “Esc” on it). The file menu will disappear.
Save As Here’s how to create a program called JOE, then create a variant of it called JOE2.
First, type the JOE program and save it. Then edit that program, choose Save As from the file menu (by pressing the A key), and type “JOE2” (and press ENTER).
Exit When you’ve finished using QBASIC, choose Exit from the file menu, by pressing the X key.
(If you didn’t save the program you worked on, the computer asks, “Save it now?” If you want to save the program you worked on, press the Y key; if you do not want to save the program you worked on, press the N key instead.)
Then the computer will exit from QBASIC, and the screen will say:
Become an expert
Congratulations! You’ve learned how to program!
C’mon, write some programs! It’s easy! Try it. You’ll have lots of fun!
A person who writes a program is called a programmer. Congratulations: you’re a programmer!
Write several programs like the ones I’ve shown you already. Then you can put on your resumé that you have “a wide variety of programming experience”, and you can talk your way into a programming job!
The rest of this chapter explains how to become a good programmer.
Programming the computer is like driving a car: the only way to become an expert is to put your hands on that mean machine and try it yourself.
If you have access to a computer, put this book next to the computer’s keyboard. At the end of each paragraph, type the examples and look, look, see the computer run. Invent your own variations: try typing different numbers and strings. Invent your own programs: make the computer print your name or a poem; make it solve problems from your other courses and the rest of your life. The computer’s a fantastic toy. Play with it.
If you’re a student, don’t wait for your instructor to give lectures and assign homework. Act now. You’ll learn more from handling the computer than from the lectures or readings. Experience counts.
Let me tell you the story of Charlie:
Be like Charlie. Hang around your computer. Communicate with it every day. At first, that will be even harder than talking with a cat or a tree, because the computer belongs to a different species, a different kingdom; but keep trying. Get to know it as well as you know your best friend.
If you’re taking a French course, you might find French difficult; and if you’re taking a computer course, you might find computers difficult also. But even a stupid three-year-old French kid can speak French, and even kindergarten kids can program the computer. They have just one advantage over you: practice!
In science fiction, computers blow up; in real life, they never do. No matter what keys you press, no matter what commands you type, you won’t hurt the computer. The computer is invincible! So go ahead and experiment. If it doesn’t like what you type, it will gripe at you, but so what?
When you try using the computer, you’ll have trouble — because you’re making a mistake, or the computer is broken, or the computer is weird and works differently from the majority computers discussed in this book. (Each computer has its own “personality”, its own quirks.)
Whenever you have trouble, laugh about it, and say, “Oh, boy! Here we go again!” (If you’re Jewish, you can say all that more briefly, in one word: “Oy!”) Then get some help.
For help with your computer, read this book! For further help, read the beginner’s manual that came with your computer, or ask the genie who gave you the computer (your salesperson or parent or boss or teacher or friend).
If you’re sitting near computers in your office, school, or home, and other people are nearby, ask them for help. They’ll gladly answer your questions because they like to show off and because the way they got to know the answers was by asking.
Computer folks like to explain computers, just as priests like to explain religion. Remember: you’re joining a cult! Even if you don’t truly believe in “the power and glory of computers”, at least you’ll have a few moments of weird fun. So play along with the weird computer people, boost their egos, and they’ll help you get through your initiation rite. Above all, assert yourself, and ask questions. “Shy guys finish last.”
When dealing with the computer and the people who surround it, be friendly but also assertive. To make sure you get your money’s worth from a computer course, ask your teacher, classmates, lab assistants, and other programmers questions, questions, questions! If you’re using a computer that you own, get help from the person who gave it to you.
Your town probably has a computer club. (To find out, ask the local schools and computer stores.) Join the club, and tell the members you’d like help with your computer. Probably some computer hobbyist will help you.
And remember — you can call me anytime at 603-666-6644, and I’ll help you, free!
Going & stopping
You can control how your computer goes and stops.
Suppose you’ve saved a QBASIC program called JOE.
To use JOE, you could turn on the computer, then say —
then choose Open from the file menu, then type “joe” (and press ENTER).
Here’s a faster way! Turn on the computer, then say:
That makes the computer use QBASIC and instantly open JOE.
Qbasic /run Try saying this:
That makes the computer use QBASIC, instantly open JOE, and automatically run JOE. Moreover, if JOE’s bottom line says “SYSTEM”, like this —
the computer will automatically exit from QBASIC when JOE finishes running.
Batch files If you wish to combine the power of QBASIC with the power of MS-DOS, here’s the trick:
Learn MS-DOS fundamentals (on pages 118-130). Then read about “Batch files” (on pages 130), and practice the examples.
In a batch file, you can make one of the lines say:
Then whenever you run that batch file, the computer will automatically run JOE.
Read about AUTOEXEC.BAT (on pages 135-137). If your AUTOEXEC.BAT file includes a line saying “qbasic /run joe”, the computer will automatically run JOE every time you turn on the computer.
If you say SLEEP, the computer will take a nap:
The second line makes the computer announce:
The next line says SLEEP, which makes the computer take a nap. The computer will continue sleeping until you wake it up by pressing a key on the keyboard. (Press any key, such as ENTER.) Then the computer, woken up, will finish running the rest of the program, whose bottom line makes it say:
Valentine’s Day This program lets the computer gripe about how humans treated it on Valentine’s Day:
Lines 2-4 make the computer say:
The next line (SLEEP) makes the upset computer go to sleep and refuse to talk to humans, until a human presses a key. When a human finally presses a key, the computer wakes up and says:
Timed pause Instead of letting the computer sleep a long time, you can set an alarm clock so the computer will be forced to wake up soon. For example, if you say SLEEP 6 (instead of just SLEEP), the computer will sleep for just 6 seconds.
That’s how to make the computer pause for 6 seconds. Give that 6-second pause before you reveal the punch line of a joke:
That program makes the computer print the joke’s setup (“Human, your intelligence is amazing! You must be an M.D.”), then pause for 6 seconds, then reveal the joke’s punch line, so the screen finally shows:
SLEEP 6 makes the computer sleep until it gets woken up by either the alarm clock (after 6 seconds) or the human (by pressing a key). If you want the computer to pause for 10 seconds instead of 6, say SLEEP 10 instead of SLEEP 6. The number after the word SLEEP can be 6 or 10 or any other positive whole number, but not a decimal.
This program makes the computer brag, then confess:
The computer begins by bragging:
But then the computer pauses for 10 seconds and finally admits:
This program makes the computer change its feelings, in surprising ways:
The computer will print —
then pause 3 seconds and change it to —
then pause 4 seconds and change it to —
then pause 5 seconds and change it to —
then pause 6 seconds and change it to —
then pause 7 seconds and change it to —
Experiment: invent your own jokes, and make the computer pause before printing the punch lines.
Speed-reading test This program tests how fast you can read:
When you run that program, the computer makes the screen display this message:
Then the computer pauses for 1 second (because of the SLEEP 1), then erases the screen (CLS). So the message appears on the screen for just 1 second before being erased!
If you manage to read that entire message in just 1 second, you’re indeed a fast reader!
But don’t stop at that first success! For the ultimate challenge, try running this program:
That makes the computer display this tongue-twister —
then pause for 2 seconds, then erase the screen. During the 2 seconds while that tongue-twister appears on the screen, can you recite the entire twister out loud? Try it! If you don’t recite it properly, you’ll sound like a mumbling moron yourself!
This program makes the computer print the word “love” once:
This fancier program makes the computer print the word “love” three times:
When you run that program, the computer will print:
Let’s make the computer print the word “love” many times. To do that, we must make the computer do this line many times:
To make the computer do the line many times, say “DO” above the line and say “LOOP” below it, so the program looks like this:
As you can see, put the line being repeated (PRINT “love”) between the words DO and LOOP and indent it. (To indent, press the SPACE bar twice. To remove an indentation, put yourself just after the indentation and then press the BACKSPACE key.) When you run that program, the computer will do PRINT “love” many times and print:
The computer will print “love” on every line of your screen.
But even when the screen is full of “love”, the computer won’t stop: the computer will try to print even more loves onto your screen! The computer will lose control of itself and try to devote its entire life to making love! The computer’s mind will spin round and round, always circling back to the thought of making love again!
Since the computer’s thinking keeps circling back to the same thought, the computer is said to be in a loop. In that program, the DO means “do what’s underneath and indented”; the LOOP means “loop back and do it again”. The lines that say DO and LOOP — and the lines between them — form a loop, which is called a DO loop.
To stop the computer’s lovemaking madness, you must give the computer a “jolt” that will put it out of its misery and get it out of the loop. To jolt the computer out of the program, abort the program.
Here’s how to abort the program: while holding down the Ctrl key, tap the PAUSE/BREAK key, which is the last key in the top row. (If your keyboard is modern, that key says PAUSE and BREAK on it. If your keyboard is old-fashioned, that key says SCROLL LOCK and BREAK on it.) That makes the computer stop running your program; it will break out of your program; it will abort your program and show you the blue screen so you can edit the program.
In that program, since the computer tries to go round and round the loop forever, the loop is called infinite. The only way to stop an infinite loop is to abort it.
Semicolon For more lovely fun, put a semicolon after “love”, so the program looks like this:
The semicolon makes the computer print “love” next to “love”, so the screen looks like this:
If you put a space after love, like this —
the computer will put a space after each love:
Bigger DO loop Run this program:
Lines 3 & 4 (which say PRINT “dog” and PRINT “cat”) make the computer print “dog” and then print “cat” next to it. Since those lines are between the words DO and LOOP, the computer does them repeatedly — PRINT “dog”, then PRINT “cat”, then PRINT “dog” again, then PRINT “cat” again — so the screen looks like this:
The computer will keep printing “dog” and “cat” until you abort the program by doing this: while holding down the Ctrl key, tap the PAUSE/BREAK key.
Blinking Let’s make the screen say “Stop pollution!” and make that message blink.
To do that, flash “Stop pollution!” onto the screen for 2 seconds, then turn that message off for 1 second (so the screen is blank), then flash that message on again. Here’s the program:
The top line (WIDTH 40) makes sure all characters appear dramatically huge.
Lines 3 & 4 (which say PRINT “Stop pollution!” and SLEEP 2) flash the message “Stop pollution!” onto the screen and keep it on the screen for 2 seconds. The next pair of lines (CLS and SLEEP 1) make the screen become blank for 1 second. Since those lines are all between the words DO and LOOP, the computer does them repeatedly — flash message then blank, flash message then blank, flash message then blank — so your screen becomes a continually flashing sign.
The screen will keep flashing until you abort the program by doing this: while holding down the Ctrl key, tap the PAUSE/BREAK key.
Instead of saying “Stop pollution!”, edit that program so it flashes your favorite phrase instead, such as “Save the whales!” or “Marry me!” or “Keepa youse hands offa my computer!” or “Jesus saves — America spends!” or “In God we trust — all others pay cash” or “Please wait — Dr. Doom will be with you shortly” or “Let’s rock!” or whatever else turns you on. Make the computer say whatever you feel emotional about. Like a dog, the computer imitates its master’s personality. If your computer acts “cold and heartless”, it’s because you are!
In the program, you typed just a few lines; but since the bottom line said LOOP, the computer does an infinite loop. By saying LOOP, you can make the computer do an infinite amount of work. Moral: the computer can turn a finite amount of human energy into an infinite amount of good. Putting it another way: the computer can multiply your abilities by infinity.
Computerized copier Suppose you want to send this poem to all your friends:
Type this program:
Since it says LPRINT instead of PRINT, it prints each copy on paper instead of on the screen. Since the LPRINT lines are in a DO loop, the computer prints the poem again and again, many times, until you abort the program — or the printer runs out of paper.
Each time the computer prints a copy of the poem, the “LPRINT CHR$(12);” makes the computer eject a sheet of paper, so each copy of the poem is on a separate page.
Before running that program, put into the printer just as many sheets of paper as you want copies. If you put in too many sheets of paper, you’ll get more copies than you want, and you’ll waste paper.
You can number the lines in your program. For example, instead of typing —
you can type:
Then when you’re discussing your program with another programmer, you can talk about “line 3” instead of having to talk about “the line in the middle of the DO loop”.
Selective numbering You can number just the lines you’re planning to discuss.
For example, if you’re planning to discuss just lines 2 and 4, you can number just those lines:
Or if you prefer, number them like this:
Decimal numbers Here’s a simple program:
Suppose you want to edit it and insert an extra numbered line between 1 and 2. QBASIC lets you give the extra line a decimal number, such as 1.5:
Number by tens Instead of making line numbers be 1, 2, 3, etc., make the line numbers be 10, 20, 30, etc., like this:
Then you can insert an extra line without using decimals:
This program makes the computer print the words “dog” and “cat” repeatedly:
It makes the computer print:
This program does the same thing:
The second line (which is numbered 10) makes the computer print “dog”. The next line makes the computer print “cat”. The bottom line makes the computer GO back TO line 10, so the computer will print “dog” again, then “cat again”, then GO back TO line 10 again, then print “dog” again, then “cat” again, etc. The computer will print “dog” and “cat” repeatedly, until you abort the program by pressing SHIFT with PAUSE/BREAK.
This program does the same thing:
The second line (named “joe”) makes the computer print “dog”. The next line makes the computer print “cat”. The bottom line makes the computer GO back TO the line named “joe”. In that program, “joe” is called the second line’s label.
One word In QBASIC, “GOTO” is one word. You’re supposed to type “GOTO”, not “GO TO”. When you press the ENTER key at the end of the line, the computer will automatically turn any “GO TO” into “GOTO”.
Skip ahead Did you ever dream about having a picnic in the woods? This program expresses that dream:
It makes the computer print:
Let’s turn that dream into a nightmare where we all become giant termites. To do that, insert the shaded items:
The computer begins by printing “Let’s munch”. Then the computer does GOTO 10, which makes the computer GO skip down TO line 10, which prints “the trees!” So the program makes the computer print just this:
Is GOTO too powerful? The word GOTO gives you great power: if you say GO back TO line 10, the computer will create a loop (as if you’d said DO...LOOP); if you say GO skip down TO line 10, the computer will skip over several lines of your program.
Since the word GOTO is so powerful, programmers fear it! Programmers know that the slightest error in using that powerful word will make the programs act very bizarre! Programmers feel more comfortable using milder words instead (such as DO...LOOP), which are safer and rarely get botched up. Since the word GOTO is scary, many computer teachers prohibit students from using it, and many companies fire programmers who say GOTO instead of DO...LOOP.
But saying GOTO is fine when you’ve learned how to control the power! Though I’ll usually say DO...LOOP instead of GOTO, I’ll say GOTO in certain situations where saying DO...LOOP would be awkward.
Life as an infinite loop
A program that makes the computer do the same thing again and again forever is an infinite loop.
Some humans act just like computers. Those humans do the same thing again and again.
Every morning they GOTO work, and every evening they GOTO home. GOTO work, GOTO home, GOTO work, GOTO home,… Their lives are sheer drudgery. They’re caught in an infinite loop.
Go to your bathroom, get your bottle of shampoo, and look at the instructions on the back. A typical bottle has three instructions:
Those instructions say to lather, then rinse, then repeat — which means to lather again, then rinse again, then repeat again — which means to lather again, then rinse again, then repeat again.… If you follow those instructions, you’ll never finish washing your hair! The instructions are an infinite loop! The instructions are a program: they program you to use lots of shampoo! That’s how infinite loops help sell shampoo.
To make the computer skip the bottom part of your program, say END:
When you run that program (by pressing SHIFT with F5), the computer will print “She smells” and then end, without printing “of perfume”.
Suppose you write a program that prints a long message, and you want to run the program several times (so several of your friends get the message). If one of your friends would be offended by the end of your message, send that friend an abridged message! Here’s how: put END above the part of the message that you want the computer to omit — or skip past that part by saying GOTO.
In your program, a line can contain several statements separated by colons, like this:
When you run that program, the computer will CLear the Screen, then PRINT “I dream”, then PRINT “of you”. Altogether, the computer will print:
If you want to number the line, put the number at the left margin, like this:
|all content copyright © 2002 / web design by josh feingold|