Enter BASIC

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.

Hereís how to program the computer by using those BASIC words.

Microsoft BASIC

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.

Whatís QBASIC?

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 versions of Microsoft BASIC is QBASIC. Itís a simplification of another version of Microsoft BASIC, called Quick BASIC.

QBASIC comes free when you buy MS-DOS 5, 6, 6.2, 6.21, or 6.22. It also comes free when you buy 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 youíre using Windows 95, you can use QBASIC if your computer used to have MS-DOS 5, 6, 6.2, 6.21, or 6.22 or if your copy of Windows 95 came on a CD-ROM disk.

Whatís in this chapter?

This chapter explains how to use QBASIC. The chapter ends with an appendix (on pages 438-449) explaining how other versions of BASIC differ.

The chapter includes 8 sections:

Page

Fun

Enter BASIC 330

Math 334

Strings 336

Tricky printing 337

File menu 339

Become an expert 340

Going & stopping 341

Using variables

Whatís a variable? 346

INPUT 349

IF 353

SELECT 355

Fancy IF conditions 357

Exiting a DO loop 359

FORÖNEXT 360

DATAÖREAD 363

Variables & constants 369

Loop techniques 370

Helpful hints

Debugging 374

Error messages 375

PAUSE key 376

F keys 376

Apostrophe 377

Pretty output

Zones 378

TAB 380

LOCATE 382

Pixels 382

Sounds 385

PRINT USING 386

Fancy calculations

Exponents 388

Contrasts 389

Clock 390

Stripping 392

Random numbers 394

Character codes 398

String analysis 400

Trigonometry 402

Types of numbers 403

Sub stuff

Subscripts 408

SUB procedures 412

Style

Design a program 416

Make it efficient 418

Test it 418

Document it 419

Weird features

Fancy input 420

SWAP 423

Sequential data files 426

Random access 428

Create a database 430

SHELL 434

ON ERROR GOTO 435

Memory cells 437

Versions of BASIC

QBASIC 438

Quick BASIC 438

Visual BASIC for DOS 439

Visual BASIC for Win. 440

GWBASIC 444

Other computers 449

QBASICís commands are explained on these pages:

Command What the computer will do Page Similar to

BEEP hum for a quarter of a second 385 SOUND, PLAY

CASE "fine" if SELECTed is "fine", do indented lines 355 SELECT, IF

CIRCLE (100, 100), 40 draw a circle at (100, 100) with radius 40 383 LINE, PAINT

CLOSE put finishing touches on the data files 426 OPEN

CLS clear the screen, so it becomes all black 332 NEW, LOCATE

COMMON SHARED x make xís box be shared among all procedures 414 DIM SHARED

DATA meat, potatoes use this list of data: meat, potatoes 363 READ, RESTORE

DATE$ = "01-24-1996" set the clock/calendar to 01-24-1996 390 TIME$ =

DECLARE SUB insult () prepare to use SUB procedure called "insult" 412 SUB

DEF SEG = 0 use memory segment #0 437 POKE

DEFDBL A-Z make ordinary variables be double-precision 404 DEFLNG, DEFINT

DEFINT A-Z make ordinary variables be short integers 404 DEFLNG, DEFDBL

DEFLNG A-Z make ordinary variables be long integers 404 DEFINT, DEFLNG

DIM record AS STRING * 20 make the record be a 20-character string 428 TYPE, DIM

DIM SHARED y$(20) make y$ be 20 strings that procedures share 414 COMMON, SUB

DIM x$(7) make x$ be a list of 7 strings 408 x =

DO do the indented lines below, repeatedly 342 DO UNTIL, LOOP

DO UNTIL EOF(1) repeat the indented lines until end of file#1 427 DO, LOOP UNTIL

ELSE do indented lines when IF conditions false 354 IF, ELSEIF

ELSEIF age < 100 THEN do lines when earlier IFs false & age < 100 354 IF, ELSE

END skip the rest of the program 345 STOP, SYSTEM

END IF make this the bottom of an IF statement 354 IF, ELSE

END SELECT make this the bottom of SELECT statement 355 SELECT, CASE

END SUB make this the bottom of a SUB procedure 412 SUB

END TYPE make this the bottom of a TYPE statement 428 TYPE

EXIT DO skip down to the line thatís under LOOP 359 DO, LOOP

EXIT SUB skip down to the END SUB line 430 END SUB

FILES print the names of all the hard diskís files 434 SHELL, NAME

FOR x = 1 TO 20 repeat the indented lines, 20 times 360 NEXT, DO

GET 1, 7, record from file#1, get the 7th record 428 PUT, INPUT #1

GOTO 10 skip to line 10 of the program 344 DO, EXIT DO

IF y < 18 THEN PRINT "m" if y is less than 18, print an "m" 353 ENDIF, ELSE

INPUT "What name"; n$ ask "What name?" and get answer n$ 349 LINE INPUT

INPUT #1, a$ input from file#1 the value of a$ 426 INPUT

KILL "joe.bas" erase the file JOE.BAS from your hard disk 434 FILES, SHELL

LINE (0, 0)-(100, 100) draw a line from (0, 0) to (100, 100) 383 PSET, CIRCLE

LINE INPUT "Type it"; n$ say "Type it" and grab whole line as input 421 INPUT

LOCATE 3, 7 move to the screenís 3rd line, 7th position 382 PRINT, CLS

LOOP make this the bottom line of a DO loop 342 LOOP UNTIL

LOOP UNTIL guess$ = "p" do the loop repeatedly, until guess$ is "p" 359 LOOP, DO UNTIL

LPRINT 2 + 2 print, onto paper, the answer to 2 + 2 338 PRINT

MID$(a$, 2)="owl" change the middle of a$ to "owl" 400 x =

NAME "joe.bas" AS "f.bas" find the file JO.BAS and rename it F.BAS 434 FILES, SHELL

NEXT make this the bottom line of a FOR loop 360 FOR

ON ERROR GOTO 1000 if the lines below cause errors, go to line 1000 435 RESUME

OPEN "jo" FOR OUTPUT AS 1 create a data file called "JO"; output to it 426 CLOSE

PAINT (100, 101) fill in the shape that surrounds (100, 101) 383 LINE, CIRCLE

PLAY "c d g# b- a" play this music: C, D, G sharp, B flat, A 385 SOUND, BEEP

POKE 1047, 224 into memory cell #1047, put 224 437 DEF SEG, x =

PSET (100, 100) make pixel (100, 100) turn white 383 SCREEN, LINE

PRINT 4 + 2 print the answer to 4 + 2 332 PRINT USING

PRINT USING "##.#"; x print x, rounded to one decimal place 386 PRINT

PRINT #1, "eat" print onto file#1 the word "eat" 426 PRINT, LPRINT

PUT 1, 7, record in file#1, change the 7th record 428 GET, PRINT #1

RANDOMIZE TIMER make random numbers be unpredictable 394 x =

READ a$ get a string from the DATA and call it a$ 363 DATA, RESTORE

RESTORE 10 skip to line 10 of the DATA 366 READ, DATA

RESUME 10 end the error trap, by going to line 10 435 ON ERROR GO TO

SCREEN 12 use video mode 12 so you get VGA graphics 383 PSET, WIDTH

SELECT CASE a$ analyze a$ to select a case from list below 355 END SELECT, IF

SHELL "ver" do this DOS command: "ver" 434 SYSTEM

SOUND 440, 18.2 make a sound of 440 hertz, for 1 second 385 PLAY, BEEP

SLEEP pause until you press a key 341 FOR

STOP skip rest of program; show the blue screen 368 END, SYSTEM

SUB insult make the lines below define "insult" 412 END SUB

SWAP x, y make x and y swap values with each other 423 x =

SYSTEM skip rest of program; try to show "C:\>" 368 END, STOP

TIME$ = "13:45:07" set the clock to 7 seconds after 13:45 390 DATE$ =

TYPE combination make "combination" be a type of variable 428 END TYPE

WIDTH 40 on screen make each character twice as wide 338 CLS

x = 47 make x stand for the number 47 346 INPUT, POKE

QBASICís functions are explained on these pages:

Function Meaning Value Page Similar to

ABS(-3.89) absolute value of -3.89 3.89 392 SGN

ASC("A") ASCII code number for A 65 399 CHR$

ATN(1) / degrees arctangent of 1, in degrees 45 402 TAN

CHR$(164) character whose code# is 164 "ñ" 398 ASC

CINT(3.89) round to nearest integer 4 392 INT, FIX

COS(60 * degrees) cosine of 60 degrees .5 402 SIN, TAN

DATE$ todayís date varies 390 TIME$

EOF(1) test whether at end of file#1 varies 427 LOF, ERR

ERR the errorís code number varies 436 EOF

EXP(1) e raised to the first power 2.718282 388 LOG, SQR

FIX(3.89) erase digits after decimal point 3 392 INT, CINT

INPUT$(4) 4 characters that are input varies 421 INSTR

INSTR("needed", "ed") position of "ed" in "needed" 3 401 other INSTR

INSTR(4,"needed","ed") search from the 4th character 5 401 other INSTR

INT(3.89) round down to a lower integer 3 392 FIX, CINT

LCASE$("We love") lower case; uncapitalize "we love" 400 UCASE$

LEFT$("smart", 2) left 2 characters of "smart" "sm" 400 RIGHT$, MID$

LEN("smart") length of "smart" 5 400 RIGHT$, MID$

LOC(1) location of record in file#1 varies 429 LOF

LOF(1) length of file#1, in bytes varies 427 EOF

LOG(2.718282) logarithm base e 1 389 EXP

LTRIM$(" Sue Smith") delete beginning spaces "Sue Smith" 401 RTRIM$

MID$("smart", 2) begin at the 2nd character "mart" 400 other MID$

MID$("smart", 2, 3) begin at the 2nd take 3 "mar" 400 other MID$

PEEK(49837) peek at memory cell #49837 varies 437 TIMER, RND

RIGHT$("smart", 2) rightmost 2 characters "rt" 400 LEFT$, MID$

RND random decimal varies 394 TIMER

RTRIM$("Sue Smith ") delete ending spaces "Sue Smith" 401 LTRIM$

SGN(-3.89) sign of -3.89 -1 392 ABS, FIX

SIN(30 * degrees) sine of 30 degrees .5 402 COS, TAN

SQR(9) square root of 9 3 388 EXP, LOG

STR$(81.4) turn 81.4 into a string " 81.4" 401 VAL

STRING$(5, "b") a string of 5 bís "bbbbb" 401 other STRING$

STRING$(5, 98) 98th ASCII character, 5 times "bbbbb" 401 other STRING$

TAN(45 * degrees) tangent of 45 degrees 1 402 ATN

TIME$ current time of day varies 390 TIMER, DATE$

TIMER # of seconds since midnight varies 390 TIME$, DATE$

UCASE$("We love") capitalize "We love" "WE LOVE" 400 LCASE$

VAL("52.6") remove the quotation marks 52.6 401 STR$

QBASICís F keys are explained on these pages:

F key Name What the computer will do Page

F2 SUB show a different SUB 412

F4 View switch between blue & black screens 333

F5 Continue continue running the program 376

SHIFT F5 Run run the entire program 332

F6 Immediate move to & from immediate window 376

F7 Above run the program down to here 377

F8 Step run one more line of the program 377

F9 Breakpoint create or destroy a red breakpoint 376

Start QBASIC

To start using QBASIC, turn on the computer without any floppy in drive A.

Make sure the screen shows this standard C prompt:

C:\>

(To make the screen show it, follow the instructions on pages 100-101 of the MS-DOS chapter. Those instructions work even if youíre using Windows 3 or 3.1 or 3.11 or 95. For more details about making Windows 95 show the C prompt, read page 160ís "DOS Commands". For more details about making earlier versions of Windows show the C prompt, read page 148ís "Choose from a menu".)

To start using QBASIC, type "qbasic" after the C prompt, so your screen looks like this:

C:\>qbasic

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:

Welcome to MS-DOS QBASIC

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 buy the CD-ROM version of Windows 95 and copy QBASIC from that CD-ROM to your hard disk as follows:

Insert the Windows 95 CD-ROM disk (which some computer makers call the "Windows 95 Companion Disk").

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 160). Then type:

copy d:other\oldmsdos\qbasic.* windows\command

(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:

CLS

PRINT 4 + 2

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:

6

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 resumé: "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:

CLS

PRINT 4 + 2

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.

Faster typing

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:

?4+2

Think of the question mark as standing for this word:

Whatís

If you want to ask the computer "Whatís 4+2", type this:

?4+2

When you run the program, the computer will print the answer, 6.

Why CLS?

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:

CLS

PRINT 79 + 2

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 ó

CLS

PRINT 79 + 2

and you do not 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:

81

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:

Syntax error

Press the ENTER key, then fix your error, then try again to run the program (by pressing SHIFT with F5 key again).

Math

This command makes the computer add 4 + 2:

PRINT 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:

6

If you want to subtract 3 from 7, type this command instead:

PRINT 7 - 3

(When typing the minus sign, do not press the SHIFT key.) The computer will print:

4

You can use decimal points and negative numbers. For example, if you type this ó

PRINT -26.3 + 1

the computer will print:

-25.3

Multiplication

To multiply, use an asterisk. So to multiply 2 by 6, type this:

PRINT 2 * 6

The computer will print:

12

Division

To divide, use a slash. So to divide 8 by 4, type this:

PRINT 8 / 4

The computer will print:

2

 

Huge & tiny numbers

When dealing with huge and tiny numbers, be careful!

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:

CLS

PRINT 200 * 300

the computer will complain by saying:

Overflow

When the computer says "Overflow", reply by pressing the ENTER key, then fix your program by inserting a decimal point, like this ó

PRINT 200 * 300.0

or like this ó

PRINT 200 * 300.

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:

PRINT 200 * 300!

When you run the program, the computer will print the right answer:

60000

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:

8.516743E+12

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:

9.23E-06

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:

.00000923

So when the computer says the answer is 9.23E-06, the computer really means the answer is:

.00000923

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:

100000000000000000000000000000000000000

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:

.00000000000000000000000000000000000001

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 ó

PRINT 2 + 3 * 4

the computer will think you mean "2 plus three fours", so it will do 2 + 4 + 4 + 4 and print this answer:

14

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 ó

PRINT 2 + 3 * 4

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:

PRINT 2 + 3 * 4

12

So the problem becomes 2 + 12, which is 14, which the computer prints.

For another example, suppose you type:

PRINT 10 - 2 * 3 + 72 / 9 * 5

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:

PRINT 10 - 2 * 3 + 72 / 9 * 5

6 40

So the problem becomes 10 - 6 + 40, which is 44, which is the answer the computer prints.

Parentheses You can use parentheses the same way as in algebra. For example, if you type ó

PRINT 5 - (1 + 1)

the computer will compute 5 - 2 and print:

3

You can put parentheses inside parentheses. If you type ó

PRINT 10 - (5 - (1 + 1))

the computer will compute 10 - (5 - 2), which is 10 - 3, and will print:

7

Strings

Letís make the computer fall in love. Letís make it say, "I love you".

Type this program:

CLS

PRINT "I love you"

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:

I love you

You can change the computerís personality. For example, if you give this command ó

PRINT "I hate you"

the computer will reply:

I hate you

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 ó

PRINT "aieee luf ya"

the computer will still copy the message (without worrying about what it means); the computer will print:

aieee luf ya

Faster typing

Instead of typing ó

PRINT "I love you"

you can type just this:

?"I love you

At the end of that line, when you press the ENTER key, the computer will automatically do three things:

The computer will replace the question mark by 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 4 + 2) do not go in quotation marks.

Accidents

Suppose you accidentally put the number 2 + 2 in quotation marks, like this:

PRINT "2 + 2"

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:

2 + 2

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:

PRINT love

Since you forgot the quotation marks, the computer thinks love is a number instead of a string; but the computer doesnít know what number it is, 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:

0

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:

I love you.

You turned me on.

Let's get married!

To make the computer say all that, just run this program:

CLS

PRINT "I love you."

PRINT "You turned me on."

PRINT "Let's get married!"

To run that program, type it and then press the F5 key. Try it!

To have even more fun, run this program:

CLS

PRINT "I long"

PRINT 2 + 2

PRINT "U"

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:

I long

4

U

Yes, the computer says it longs for you!

Tricky printing

Printing can be tricky! Here are the tricks.

Indenting

Suppose you want the computer to print this letter:

Dear Joan,

Thank you for the beautiful

necktie. Just one problem--

I don't wear neckties!

Love,

Fred-the-Hippie

This program prints it:

CLS

PRINT "Dear Joan,"

PRINT " Thank you for the beautiful"

PRINT "necktie. Just one problem--"

PRINT "I don't wear neckties!"

PRINT " Love,"

PRINT " Fred-the-Hippie"

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:

Program What the computer will do

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:

joy

sorrow

Semicolons

Run this program:

CLS

PRINT "fat";

PRINT "her"

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:

father

This program gives you some food for thought:

CLS

PRINT "I love to eat her";

PRINT "ring for dinner";

PRINT "you are the most beautiful fish in the whole sea!"

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:

I love to eat herring for dinner

you are the most beautiful fish in the whole sea!

The next program shows what happens to an evil king on a boat:

CLS

PRINT "sin"; "king"

The computer will print "sin", and will print "king" on the same line, like this:

sinking

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:

PRINT -3; "is my favorite number"

Whenever the computer prints a NUMBER, it prints a blank space afterwards; so the computer will print a blank space after -3, like this:

-3 is my favorite number

­

space

 

Spaces before positive numbers

This command tells what to put in your coffee:

PRINT 7; "do"; "nuts"

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:

7 donuts

­ ­

spaces

Hey, if youíre feeling cool, maybe this command expresses your feelings:

PRINT "the temperature is"; 4 + 25; "degrees"

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:

the temperature is 29 degrees

­ ­

spaces

 

Fix the negative numbers

Use this command if youíre even colder:

PRINT "the temperature is"; 4 - 25; "degrees"

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:

the temperature is-21 degrees

­ ­

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:

PRINT "the temperature is "; 4 - 25; "degrees"

­

inserted space, before the quotation mark

Then the computer will print:

the temperature is -21 degrees

­

inserted space

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:

PRINT 6 + 2; 6 - 2; 6 * 2; 6 / 2

That makes the computer print the four answers:

8 4 12 3

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:

CLS

LPRINT 2 + 2

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 the F5 key), the computer will compute 2 + 2 and print this answer onto paper:

4

Eject the paper manually 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).

Eject the paper automatically Instead of ejecting paper manually, you can make the computer eject paper AUTOMATICALLY, by putting this line at the bottom of your program:

LPRINT CHR$(12);

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:

CLS

LPRINT 2 + 2

LPRINT CHR$(12);

This program prints a poem on paper and then ejects the paper:

CLS

LPRINT "I see England."

LPRINT "I see France."

LPRINT "I see Batman's"

LPRINT "underpants!"

LPRINT CHR$(12);

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).

Dual printing 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:

Program What the computer will do

CLS Clear the screen.

PRINT 2 + 2 Print the answer (4) onto the screen.

LPRINT 2 + 2 Print the answer (4) onto paper.

LPRINT CHR$(12); Eject the paper from the printer.

Screen dump Hereís another way to print on paper: while youíre looking at the computerís answers (on the black screen), press the PRINT SCREEN key (which is on the computerís keyboard, near the top right corner).

That makes the computer dump onto paper a snapshot of everything thatís on the screen. The snapshot on paper is called a screen dump.

After pressing the PRINT SCREEN key, eject the paper manually.

If you want to print several copies on paper, press the PRINT SCREEN key several times ó and eject the paper manually each time.

WIDTH 40

At the top of your program, instead of saying CLS, you can say:

WIDTH 40

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).

File menu

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 ó

CLS

PRINT 2 + 2

the printer will print this onto paper:

CLS

PRINT 2 + 2

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.

Invent a name 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.

Save often 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 fifteen 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 fifteen 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:

C:\>

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.

Practice

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:

At Wesleyan Universityís computer center, one of the directors was having trouble making the computer print the universityís payroll. He asked me for help, but I said I didnít know either. I saw a little kid sitting at one of the keyboards. "Hey, Charlie," I called to him, "weíre having trouble getting the payroll out."

Little Charlie came over and typed some stuff on our keyboard. "The payroll will be out in a minute," he said gleefully.

Charlie was just in seventh grade. Heíd never taken a computer course; his school didnít offer one. But by spending the whole summer just "hanging around" our computer, he knew it better than we.

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!

Be bold

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?

Troubles

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.

Get 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 617-666-2666, and Iíll help you, free!

Going & stopping

You can control how your computer goes and stops.

Instant open

Suppose youíve saved a QBASIC program called JOE.

To use JOE, you could turn on the computer, then say ó

C:\>qbasic

then choose Open from the file menu, then type "joe" (and press ENTER).

Hereís a faster way! Turn on the computer, then say:

C:\>qbasic joe

That makes the computer use QBASIC and instantly open JOE.

Qbasic /run Try saying this:

C:\>qbasic /run joe

That makes the computer use QBASIC, instantly open JOE, and automatically run JOE. Moreover, if JOEís bottom line says "SYSTEM", like this ó

CLS

PRINT "I love you"

SYSTEM

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 page 114-130). Then read about "Batch files" (on pages 130-131), and practice the examples.

In a batch file, you can make one of the lines say:

qbasic /run joe

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.

SLEEP

If you say SLEEP, the computer will take a nap:

CLS

PRINT "I'm going to take a nap."

SLEEP

PRINT "Thanks for waking me up."

The second line makes the computer announce:

I'm going to take a nap.

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:

Thanks for waking me up.

Valentineís Day This program lets the computer gripe about how humans treated it on Valentineís Day:

CLS

PRINT "Valentine's Day, you didn't bring me flowers!"

PRINT "I won't speak until you gimme roses!"

PRINT "Bring them, then touch one of my keys."

SLEEP

PRINT "It's great to wake up and smell the roses!"

Lines 2-4 make the computer say:

Valentine's Day, you didn't bring me flowers!

I won't speak until you gimme roses!

Bring them, then touch one of my keys.

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:

It's great to wake up and smell the roses!

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:

CLS

PRINT "Human, your intelligence is amazing! You must be an M.D.";

SLEEP 6

PRINT "--Mentally Deficient!"

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:

Human, your intelligence is amazing! You must be an M.D.--Mentally Deficient!

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:

CLS

PRINT "We computers are smart for three reasons."

PRINT "The first is our VERY GOOD MEMORY."

PRINT "The other two reasonsÖ ";

SLEEP 10

PRINT "I forgot."

The computer begins by bragging:

We computers are smart for three reasons.

The first is our VERY GOOD MEMORY.

The other two reasonsÖ

But then the computer pauses for 10 seconds and finally admits:

I forgot.

This program makes the computer change its feelings, in surprising ways:

CLS

PRINT "I'm up";

SLEEP 3

PRINT "set! I want to pee";

SLEEP 4

PRINT "k at you";

SLEEP 5

PRINT "r ma";

SLEEP 6

PRINT "nual";

SLEEP 7

PRINT "dexterity. Touch me!"

The computer will print ó

I'm up

then pause 3 seconds and change it to ó

I'm upset! I want to pee

then pause 4 seconds and change it to ó

I'm upset! I want to peek at you

then pause 5 seconds and change it to ó

I'm upset! I want to peek at your ma

then pause 6 seconds and change it to ó

I'm upset! I want to peek at your manual

then pause 7 seconds and change it to ó

I'm upset! I want to peek at your manual dexterity. Touch me!

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:

CLS

PRINT "If you can read this, you read quickly."

SLEEP 1

CLS

When you run that program, the computer makes the screen display this message:

If you can read this, you read quickly.

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:

CLS

PRINT "Mumbling morons make my mom miss murder mysteries Monday morning."

SLEEP 2

CLS

That makes the computer display this tongue-twister ó

Mumbling morons make my mom miss murder mysteries Monday morning.

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!

DOÖLOOP

This program makes the computer print the word "love" once:

CLS

PRINT "love"

This fancier program makes the computer print the word "love" three times:

CLS

PRINT "love"

PRINT "love"

PRINT "love"

When you run that program, the computer will print:

love

love

love

Letís make the computer print the word "love" many times. To do that, we must make the computer do this line many times:

PRINT "love"

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:

CLS

DO

PRINT "love"

LOOP

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:

love

love

love

love

love

love

love

love

love

etc.

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:

CLS

DO

PRINT "love";

LOOP

The semicolon makes the computer print "love" next to "love", so the screen looks like this:

lovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelove

lovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelove

lovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelovelove

etc.

If you put a space after love, like this ó

CLS

DO

PRINT "love ";

LOOP

the computer will put a space after each love:

love love love love love love love love love love love love love love love love

love love love love love love love love love love love love love love love love

love love love love love love love love love love love love love love love love

etc.

Bigger DO loop Run this program:

CLS

DO

PRINT "dog";

PRINT "cat";

LOOP

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:

dogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcat

dogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcat

dogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcat

etc.

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:

WIDTH 40

DO

PRINT "Stop pollution!"

SLEEP 2

CLS

SLEEP 1

LOOP

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:

I'm having trouble

with my nose.

The only thing it does is:

Blows!

Type this program:

CLS

DO

LPRINT "I'm having trouble"

LPRINT "With my nose."

LPRINT "The only thing it does is:"

LPRINT "Blows!"

LPRINT CHR$(12);

LOOP

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.

Line numbers

You can number the lines in your program. For example, instead of typing ó

CLS

DO

PRINT "love"

LOOP

you can type:

1 CLS

2 DO

3 PRINT "love"

4 LOOP

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:

CLS

2 DO

PRINT "love"

4 LOOP

Or if you prefer, number them like this:

CLS

1 DO

PRINT "love"

2 LOOP

Decimal numbers Hereís a simple program:

1 CLS

2 PRINT "Life's a blast!"

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:

1 CLS

1.5 PRINT "I hope..."

2 PRINT "Life's a blast!"

Number by tens Instead of making line numbers be 1, 2, 3, etc., make the line numbers be 10, 20, 30, etc., like this:

10 CLS

20 PRINT "Life's a blast!"

Then you can insert an extra line without using decimals:

10 CLS

15 PRINT "I hope..."

20 PRINT "Life's a blast!"

GOTO

This program makes the computer print the words "dog" and "cat" repeatedly:

CLS

DO

PRINT "dog";

PRINT "cat";

LOOP

It makes the computer print:

dogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcat

dogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcat

dogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcatdogcat

etc.

This program does the same thing:

CLS

10 PRINT "dog";

PRINT "cat";

GOTO 10

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:

CLS

joe: PRINT "dog";

PRINT "cat";

GOTO joe

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:

CLS

PRINT "Let's munch"

PRINT "sandwiches under"

PRINT "the trees!"

It makes the computer print:

Let's munch

sandwiches under

the trees!

Letís turn that dream into a nightmare where we all become giant termites. To do that, insert the shaded items:

CLS

PRINT "Let's munch"

GOTO 10

PRINT "sandwiches under"

10 PRINT "the trees!"

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:

Let's munch

the trees!

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:

Lather.

Rinse.

Repeat.

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.

END

To make the computer skip the bottom part of your program, say END:

CLS

PRINT "She smells"

END

PRINT "of perfume"

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.

Multi-statement line

In your program, a line can contain several statements separated by colons, like this:

CLS: PRINT "I dream": PRINT "of you"

When you run that program, the computer will CLear the Screen, then PRINT "I dream", then PRINT "of you". Altogether, the computer will print:

I dream

of you

If you want to number the line, put the number at the left margin, like this:

10 CLS: PRINT "I dream": PRINT "of you"