QBASIC

Pages 330-437 explained QBASIC, which comes with MS-DOS versions 5, 6, 6.2, 6.21, and 6.22.

Here’s how other versions of BASIC differ.…

Quick BASIC

Quick BASIC understands a few more commands than QBASIC. Quick BASIC is available for MS-DOS and for Apple Macintosh computers, but the Apple Macintosh version is inferior.

When you start using Quick BASIC (version 4.5 for MS-DOS), you’ll encounter these differences from QBASIC.…

Start BASIC (page 332)

When you buy Quick BASIC, it comes on five 5¼-inch floppy disks. Here’s how to copy them to your hard disk:

Turn on the computer without any floppy disk in drive A. When you see the C prompt, put Quick BASIC’s first disk (the Setup/Microsoft QB Express Disk) into drive A and type "a:".

The computer will display an A prompt. Type "setup".

The computer will say "QuickBASIC Setup Program". Press ENTER twice.

The computer will say "Easy Setup Menu". Press the I key. Press ENTER three times.

When the computer tells you, put Quick BASIC’s second disk (the Program Disk) into drive A and press ENTER. When the computer tells you, do the same for the remaining Quick BASIC disks.

Press ENTER two extra times. The computer will say "QB Express". Press R then X.

You’ll see an A prompt, like this:

A:\>

Turn off the computer, so you can start fresh.

To start using Quick BASIC, turn on the computer without any floppy disk in drive A. When you see the C prompt, type "cd qb45", like this:

C:\>cd qb45

When you press ENTER at the end of that line, the computer will say:

C:\QB45>

Type "qb" (and press ENTER). The computer will say "Untitled". (If the computer says "Welcome to QuickBASIC", press the Esc key.)

File menu (page 339)

The file menu can be short or long. The short menu offers just 5 choices; the long menu (which is called the full menu) offers 12 choices. Here’s how to switch from short menu to full menu (or back to short): tap the Alt key, then the letter O, then F.

Save (page 339)

On the file menu, one of the choices is "Save As...". Another choice, a plain "Save", appears just if the menu is full instead of short. So before choosing just "Save", make sure the menu is full.

New (page 339)

Quick BASIC’s file menu says "New Program" instead of just "New". Choose "New Program" by pressing the N key.

Open (page 339)

Quick BASIC’s file menu says "Open Program" instead of just "Open". Choose "Open Program" by pressing the letter O.

Instant open (page 341)

Suppose you’ve written a program that has no errors, and you’ve saved the program as JOE.BAS. Try this: while JOE.BAS is on the blue screen, tap the Alt key, then R, then X, then ENTER. That makes the computer create a file called JOE.EXE (and also a file called JOE.OBJ). Then you can run JOE fast by typing "joe" at the C prompt, like this:

C:\QB45>joe

Visual BASIC for DOS

Visual BASIC for DOS is newer and fancier than QBASIC & Quick BASIC.

When you start using version 2 of Visual BASIC for DOS, you’ll encounter these differences from QBASIC.…

Start BASIC (page 332)

When you buy Visual BASIC for DOS, it comes on two 3½-inch floppy disks. Here’s how to copy them to your hard disk:

Turn on the computer without any floppy in drive A. When you see the C prompt, put Visual BASIC Disk 1 into drive A and type "a:setup", so the screen looks like this:

C:\>a:setup

The computer will say "Visual Basic for MS-DOS Setup". Press ENTER.

The computer will say "Type your full name". Type your full name (or the name of the company that bought Visual BASIC) and press ENTER at the end of it.

Press ENTER six more times.

The computer will say, "Insert Disk 2". Insert it into drive A and press ENTER. (If the computer says "WARNING: Setup has found multiple linkers", press ENTER again.)

The computer will say, "To start Microsoft Visual Basic, type VBDOS at the MS-DOS prompt." Press ENTER.

You’ll see a C prompt, like this:

C:\VBDOS>

Turn off the computer, so you can start fresh.

To start using Visual BASIC, turn on the computer without any floppy disk in drive A, so that the computer says:

C:\>

If you’ve put the DO.BAT file onto your hard disk (as I recommended in the MS-DOS chapter), type "do vbdos". If you’ve not put DO.BAT onto your hard disk, type "cd vbdos", then on the line underneath type "vbdos" again (so the lower line says "C:\VBDOS>vbdos").

If the computer says "Visual Basic", press ENTER.

The computer will say "Untitled" and "Project".

While holding down the Ctrl key, press the F10 key. The word "Project" will disappear.

File menu (page 339)

Visual BASIC’s file menu gives you 14 choices, which are worded differently than QBASIC’s choices.

Instead of "Save", choose "Save File" (by pressing S).

Instead of "Save As", choose "Save File As" (by pressing A).

Instead of "Open", choose "Add File" (by pressing D).

Instead of "New", choose "New Project" (by pressing N).

If you print on paper by choosing Print from the file menu (and then pressing ENTER), the printer will eject the paper automatically.

Instant open (page 341)

Suppose you’ve written a program that has no errors, and you’ve saved the program as JOE.BAS. Try this: while JOE.BAS is on the blue screen, tap the Alt key, then R, then X, then ENTER. That makes the computer create a file called JOE.EXE (and also a file called JOE.OBJ). Then you can run JOE fast by typing "joe" at the C prompt, like this:

C:\VBDOS>joe

Print (page 412)

To print the entire program onto paper (including even the SUB procedures), press Alt then F then P then ENTER. The printer will eject the paper automatically.

Error numbers (page 436)

In Visual BASIC, error #73 is "Feature unavailable", and you can make advanced errors numbered from 80 to 480.

Visual BASIC for Windows

Visual BASIC for Windows is so different from other versions of BASIC that it shouldn’t be called "BASIC". In fact, most computer folks call it VB. That’s what I’ll call it too!

VB’s main purpose is to let you create Windows programs that do everything expected of Windows programs: by using VB, you can create Windows programs that let the human use a mouse to click on icons, choose from menus, use dialog boxes, etc.

Before trying to learn VB, you should pick up two prerequisite skills:

The first prerequisite is to learn QBASIC, which is much easier than VB. I explained QBASIC on pages 330-437. Read and practice that material.

The second prerequisite is to play with Windows software (such as a Windows word-processing program), so you have some idea of how a Windows program should act. I explained the fundamentals of Windows software on pages 95-113 and pages 162-175. Read and practice whichever of those Windows products you have access to.

After you’ve had those experiences, read the following technical notes, which were written mainly by my research assistant, Len Pallazola, and edited by me. They explain VB’s "Standard Version 4". (Other versions are similar.)

I plan to put more details in future editions. To get on the mailing list, use the coupon on the back page or phone me at 617-666-2666.

VB’s philosophy

To write a program by using VB, you start by designing the way you want the screen to look. During that design process, you use your mouse, as if you were designing a work of art by using a graphics program.

Once you’ve made a beautiful-looking screen that has pretty icons, menus, windows, and dialog boxes, you tell the computer what to do if the human clicks on one of those objects. For each object on the screen, you define a procedure (subroutine) for the computer to obey if the human clicks the object.

And now, some jargon:

The general appearance of the screen you created is called your program’s interface. The objects that you expect the human to click (such as buttons, menus, check boxes, and text boxes) are called controls. You add the controls to windows called forms.

While the program is running, not much happens until the human performs an event (such as clicking on a button, typing on the keyboard, or picking an item from a menu). That’s called an event-driven program. All the controls on the screen sit there waiting patiently for the human to perform an event that makes one of the subroutines come to life.

How to install VB

VB’s Standard Version 4 requires Windows 95 and costs just $87 from discount dealers (such as Computer Discount Warehouse at 800-726-4239).

It comes on a CD-ROM disk. Here’s how to copy it to your hard disk:

Start the computer, so the Start Button is on the screen. Insert the Visual BASIC Standard Edition CD-ROM disk.

Click the Start button, then Run. Type "d:setup" or "e:setup" (and press ENTER).

The computer will say "Welcome to the Microsoft Visual BASIC 4.0 - 32-bit installation program". Press ENTER.

The computer will say "Enter your full name". If your name’s not on the screen yet, type your full name (such as Susan B. Anthony), then press TAB, then type the name of your organization (if any). Press ENTER twice.

Type the CD Key (which is the 10-digit number that’s on the orange stick on the back of the square jewel box the CD-ROM came in), then press ENTER.

The computer will show you a 20-digit Product Identification Number. Using a pen, scribble that number onto the registration card (which is black, white, and yellow). Press ENTER five times.

The computer will say "Microsoft Visual BASIC 4.0 — 32-bit Setup was completed successfully." Press ENTER.

Start VB

To start running VB, click Start, then Programs, then "Visual Basic 4.0", then "Visual Basic 4.0" again.

When VB opens, you’ll see several separate and confusing windows. Since none of the windows covers the whole screen, you can still see parts of your Windows 95 desktop peeking through. Don’t worry — it’s supposed to look that way.

Like most Windows programs, VB includes a title bar, a menu bar, and a toolbar at the screen’s top. In the screen’s middle, you’ll see a dotted window called Form1. Form1 will eventually become the main window of your VB program. Right now, it’s blank.

On your screen’s left side, you’ll see this toolbar:

Pointer Picture Box

Label Text Box

Frame Command Button

Check Box Option Button

Combo Box List Box

Horiz. Scroll Bar Vertical Scroll Bar

Timer Drive List Bbox

Directory List Box File List Box

Shape Line

Image Data

OLE Common Dialog

Data-Bound List Data-Bound Combo

Data-Bound Grid

Microsoft calls that toolbar the Toolbox, but let’s call it the control palette instead, since it contains the many controls you can add to your VB programs. Controls are the tools that make your program do things. Some common controls are command buttons, text boxes, and scroll bars.

On your screen’s right-hand side, you’ll see two smaller windows. The top one, which says "Project1", is called the Project window because it lists all files making up your VB program. The Properties window is directly below the Project window. Each control and form has its own set of unique characteristics, called properties.

Create your program

Now you’re ready to design your first program!

Step 1: create a button Double-click the Command Button tool on the control palette. That creates a button (called Command1) in the middle of your form.

Step 2: create the button’s subroutine Double-click the button you created. That makes a window appear, which says:

Private Sub Command1_Click()

End Sub

That text is the subroutine that runs when the button is clicked. The top line is the subroutine’s header. The bottom line is the subroutine’s footer, which tells the computer that the subroutine has ended.

The subroutine doesn’t do anything yet, because the line between the header and footer is blank. Change the subroutine so it says:

Private Sub Command1_Click()

MsgBox "Welcome, human!"

End

End Sub

In that subroutine, the MsgBox line tells the computer to say this message: "Welcome, human!" The End line tells the computer to automatically close the program when it finishes.

Step 3: run your program Go ahead: run your program by pressing the F5 key.

Your form appears, with the Command1 button in the middle. Click that button.

If you typed the subroutine correctly, you’ll see a message box saying "Welcome, human!".

Click OK. That makes the message box go away and the program end.

Congratulations! You wrote and ran your first VB program! Now you can put on your résumé that you’re a VB programmer!

Properties window

Each object in your program has its own set of properties that control how it looks and functions. Lets change the properties of the Command1 button you created in the previous example.

First, click on the Command1 button just once. That makes Command1 the active control, so the Properties window shows Command1’s properties. Click Caption in the Properties window, then type:

Hello

Now the command button says "Hello" instead of "Command1".

Click Font in the Properties window. The font used by the button is shown in the font box. Next to the font name is a small gray button with three dots (…). Click that small button. The font dialog box appears. Change Font style to Bold, then click OK. Now your button boldly says "Hello"!

Change properties by coding

You can change an item’s properties while the program is running. Just add the new properties into your program code.

For example, here’s how to make a button that changes each time you click it.

First, double-click the Command Button tool on the control palette. A new command button will appear on your form. Drag it (by using the mouse) so it doesn’t cover the button you created in the previous example.

Then click Caption in the Properties window and type "Click me".

Double-click the "Click me" button you just created, then type this subroutine between the header and footer lines of the button’s subroutine:

If Command2.Caption = "Click me" Then

Command2.Caption = "Again!"

Else

Command2.Caption = "Click me"

End If

Press F5 to run your program. Click the "Click me" button. If you typed the program correctly, the button text will change to "Again!" When you click it again, it changes back to "Click me".

Close your program by clicking its X box. (You must close it manually because no line said simply "End".)

Save your program

To copy your program onto your hard disk, click File, then Save Project.

Name your forms The computer will ask you to name each form in your program. You can accept the default names (Form1, Form2, etc.) or give each form a new filename. After typing a name for a form, click Save.

Name your project Each VB program includes a project file that contains information about your program. The computer will name your project "Project1". To accept that name, click Save. To pick something else, type a name first, then click Save.

InputBox

To make the computer ask a question, use an InputBox.

For example, here’s how to make a button that asks for your name.

First, double-click the Command Button tool on the control palette. A new command button will appear on your form. (Drag it so it doesn’t cover any buttons you created in previous examples.)

Click Caption in the Properties window and type "Ask me".

Then double-click the "Ask me" button you created. Type this between the header and footer lines of the button’s subroutine:

YourName = InputBox("What is your name?", "Name")

MsgBox "I adore anyone whose name is " & YourName

Run the program (by pressing F5). Click the "Ask me" button.

If you typed the program correctly, a box called Name will appear and ask "What is your name?". The computer waits for you to type your name underneath. Type your name. For example, if your name is Maria, make the screen look like this:

What’s your name?

Maria

At the end of typing "Maria", press the ENTER key (or click the OK button).

That Name box will go away, and a new box will appear that says "I adore anyone whose name is " and then your name. For example, if you said your name was "Maria", the computer will say "I adore anyone whose name is Maria".

Close your program (by clicking its X box).

College admissions Let’s make the computer print a letter admitting you to the college of your choice.

First, start a new project by clicking File then New Project.

Double-click the form, then type this between the header and footer lines of the form’s subroutine:

Form1.Show

College = InputBox("What college would you like to enter?")

Print "Congratulations!"

Print "You have just been admitted to " & College

Print "because it fits your personality."

Print "I hope you go to " & College & "."

Print " Respectfully yours,"

Print " The Dean of Admissions"

The top line, which says Form1.Show, is needed because later lines say Print. (In that program, if you wish, you can say Form1.Print instead of just Print, to emphasize that the output should be printed onto Form1 rather than some other window. But if you say just Print instead of Form1.Print, the computer will figure out what you meant anyway.)

Run the program (by pressing F5).

The subroutine’s InputBox line makes the computer ask "What college would you like to enter?" and wait for you to answer. Your answer will be called College. If you’d like to be admitted to Harvard, you’ll be pleased…

The computer asks you: What college would you like to enter?

You type: Harvard

The computer admits you: Congratulations!

You have just been admitted to Harvard

because it fits your personality.

I hope you go to Harvard.

Respectfully yours,

The Dean of Admissions

You can choose any college you wish:

The computer asks you: What college would you like to enter?

You type: Hell

The computer admits you: Congratulations!

You have just been admitted to Hell

because it fits your personality.

I hope you go to Hell.

Respectfully yours,

The Dean of Admissions

Numeric input This program makes the computer predict your future. Click File, then New Project to start a new program. Double-click the form, then type this between the subroutine’s header and footer lines:

Form1.Show

Print "I can predict what'll happen to you in the year 2000!"

Y = InputBox("In what year were you born?")

Print "In the year 2000, you'll turn " & 2000 - Y & " years old."

Here’s a sample run:

I predict what'll happen to you in the year 2000!

In what year were you born?

1962

In the year 2000, you'll turn 38 years old.

Logic

VB uses the words If and Else and colons the same way as QBASIC.

For example, let’s write a program so that if the human is less than 18 years old, the computer will say:

You are still a minor.

Here’s the program:

Form1.Show

Age = InputBox("How old are you")

If Age < 18 Then Print "You are still a minor"

Line 2 makes the computer ask "How old are you" and wait for the human to type an age. Since the symbol for "less than" is "<", the bottom line says: if the age is less than 18, then print "You are still a minor".

Go ahead! Run that program! The computer begins the conversation by asking:

How old are you?

Try saying you’re 12 years old, by typing a 12, so the screen looks like this:

How old are you?

12

When you finish typing the 12 and press the ENTER key at the end of it, the computer will reply:

You are still a minor

Try running that program again, but this time try saying you’re 50 years old instead of 12, so the screen looks like this:

How old are you?

50

When you finish typing the 50 (and press the ENTER key at the end of it), the computer will not say "You are still a minor". Instead, the computer will say nothing, since we didn’t teach the computer how to respond to adults yet.

To handle adults, change the If line to this:

If Age < 18 Then Print "You are still a minor" Else Print "You are an adult"

To create longer messages, use colons —

If Age < 18 Then Print "You are still a minor": Print "Ah, the joys of youth!"

or use a multi-line If:

If Age < 18 Then

Print "You are still a minor"

Print "Ah, the joys of youth!"

End If

Your multi-line If can include Else:

If Age < 18 Then

Print "You are still a minor"

Print "Ah, the joys of youth!"

Else

Print "You are an adult"

Print "We can have adult fun"

End If

VB also uses the words ElseIf and Select, the same way as QBASIC.

Which commands differ?

Most QBASIC commands I described in this book (such as CLS) work in VB. When typing a command in VB, capitalize just the command’s first letter, like this: Cls.

You can omit any dollar sign. If you invent a variable name, don’t put a period in the name’s middle.

Unfortunately, VB is too stupid to handle these QBASIC commands: COMMON SHARED, DATA, DEF SEG, FILES, LOCATE, PAINT, PLAY, POKE, READ, RESTORE, SCREEN, SOUND, SLEEP. SWAP, SYSTEM.

Here’s an alphabetical list of commands that work differently in VB:

Beep You may need to insert some kind of wait subroutine if you want to use several beeps in a row.

Circle (100, 100), 40 Still works, but those coordinates are pretty small. You’ll want to change it to Circle (1000, 1000), 400. Otherwise you’ll get what looks like a lower-case "o" in the top left corner of your form.

Declare Sub insult () Still exists, but now it’s used just for declaring a subroutine you’re going to use that’s in a .DLL file. You must insert "lib" and then the name of the .DLL file in order for it to work. Example: Declare Sub insult Lib "Joan.DLL".

Dim x$(7) Still works but doesn’t make x$ be a list of seven strings. Instead, it makes them variants (which can be a string, an integer, or a date). You can force them to be strings by saying Dim x$(7) As String.

Input "What name"; n$ Doesn’t work. Instead, use an InputBox, and format it like this: n$=InputBox("What name?"). Notice you must add the question mark yourself now.

Line (0, 0)-(100, 100) Still works. Add color by putting a comma, then RGB(r, g, b) where r, g, and b are red, green, and blue values of 0-255. Example: Line (0, 0)-(1000, 1000), RGB(255, 0, 0).

Line Input "Type it"; n$ Still works as input from a file, but you must put the file number first (and you can’t use the prompt). Example: Line Input #1, n$.

Lprint 2 + 2 Instead of saying "Lprint", say "Print.Printer".

Pset (100, 100) Still works but makes the pixel turn black instead of white. Add color by putting a comma, then RGB(r, g, b) where r, g, and b are red, green, and blue values of 0-255.

Print 4 + 2 Still works. The result is printed on the current form. Additional printouts are scrolled downwards. When it gets to the bottom of the form, it will not scroll everything else upwards: it’s pretty dumb. It just keeps going off the form where you can’t see it anymore.

Print Using "##.#"; x Doesn’t work. Instead, format your variable first by saying: y = format(x, "##.#") on one line, and then Print y on the next line.

Shell "ver" The Shell command still works, but this command won’t. First of all, the filename must be in parentheses and quotes ("ver"). But if you try to run Shell ("ver"), the computer says "File Not Found", because the Shell command works just with external functions or files.

Stop Still works, but the screen isn’t blue.

Sub insult Still works, but you must decide whether it’s public or private and must add the parentheses at the end. Example: Private Sub insult ().

Type combination Still works, but you must insert the word "Private" at the beginning.

Width 40 Still exists but does something different. Now it’s used to determine the width of a line in an output file. You must say "Width #1, 40" (to say that a line in file #1 is 40 characters long).

Which functions differ?

This book explained many QBASIC functions (such as SQR). Most of them work in VB. When typing a function in VB, capitalize just the function’s first letter, like this: Sqr. You can omit any dollar signs.

Unfortunately, VB is too stupid to handle INPUT$ and PEEK.

 

GWBASIC

Older versions of MS-DOS came with a more primitive BASIC, called GWBASIC. Yes, GWBASIC came with MS-DOS (and PC-DOS) versions 1, 1.1, 2, 2.1, 3, 3.1, 3.2, 3.3, and 4.

GWBASIC resembles QBASIC but omits some QBASIC commands and uses a more primitive way to edit your programs.

In earlier editions of this book, I explained GWBASIC in detail. In those editions, the BASIC tutorial emphasizes GWBASIC instead of QBASIC; most of the sample programs are written in GWBASIC. If you’re using GWBASIC, get edition 16, 17, 18, 19, or 20. Phone me at 617-666-2666 to find out which of those editions are still available and their prices.

Here’s how GWBASIC differs from QBASIC.…

Start BASIC (page 332)

Before using GWBASIC, practice using the keyboard, by giving DOS commands or using a word processor.

(QBASIC would require you to practice DOS’s edit command, but GWBASIC does not.)

The file that makes the computer understand GWBASIC is usually called "GWBASIC.EXE"; but some versions of MS-DOS (and PC-DOS) call it "BASIC.COM" or "BASICA.COM" instead. Some versions of MS-DOS lack the file altogether and can’t do BASIC.

To start GWBASIC, try typing:

C:\>gwbasic

If the computer gripes (by saying "Bad command or file name"), try typing:

C:\>basica

If the computer still gripes (by saying "Bad command or file name" again), try typing:

C:\>basic

If the computer still gripes (by saying "Bad command or file name" again), try saying "dos\gwbasic" or "dos\basica" or "dos\basic".

If you don’t have a drive C (because your computer doesn’t have a hard disk), find the floppy that contains GWBASIC.EXE or BASICA.COM or BASIC.COM. Insert that floppy into drive A and say "gwbasic" or "basica" or "basic".

When you get into GWBASIC successfully, the computer will say:

Ok

(In QBASIC, the screen would turn blue; but in GWBASIC, the screen stays black, with "Ok" appearing in white lettering.)

If the computer does not say the word "Ok", you probably used the wrong version of DOS. Make sure your version of DOS came from your computer’s manufacturer. For example, if IBM built your computer, use IBM’s PC-DOS, not a clone version of MS-DOS. If a clone company built your computer, use your clone’s version of MS-DOS, not IBM’s PC-DOS. Using the wrong version of DOS makes the computer print a strange message, the screen go crazy, or the keyboard stop working.

(If your computer’s an IBM PC Junior, buy the BASIC cartridge and put it into the left cartridge slot before you turn the computer on. That cartridge makes the Junior understand CIRCLE, PAINT, PLAY, TIMER, and the BASIC words for disks.) If your Junior is attached to a monitor instead of a TV, type "width 80" after the computer says "Ok".)

Type your program (page 332)

Instead of typing this program —

CLS

PRINT 4 + 2

type this:

10 CLS

20 PRINT 4 + 2

Notice that you must type a number (such as 10 or 20) in front of each program line. For best results, use these numbers: 10, 20, 30, etc.

Run your program (page 332)

To run your program, press the F2 key (instead of QBASIC’s SHIFT F5). When the computer finishes running the program, the computer will say:

Ok

Then to see your program again, press the F1 key (or type "list") and press ENTER.

(If your computer’s an IBM PC Junior, you must press the Fn key before pressing F2 or F1.)

Faster typing (page 333)

When you press ENTER at the end of the line, the computer will secretly convert "cls" into "CLS" and convert "print" and "?" into PRINT. To see the results of the conversion, type:

list

Why CLS? (page 333)

In GWBASIC, saying CLS is less important than in QBASIC. If you wish, omit the CLS line. When I write GWBASIC programs myself, I usually omit the CLS line.

Edit your program (page 333)

To start a new program, say:

new

That makes the computer forget about all the lines of your previous program. Then type the new program.

To delete just line 20, type this under your program:

20

That makes the computer forget about line 20.

To insert a line 15 between lines 10 and 20, type line 15 under your program. When you finish typing that line, the computer will automatically rearrange your program and put the lines in increasing order. To see the rearranged program, say "list".

To edit line 20 (instead of deleting it), use one of the following methods.…

Method 1: retype Under the program, type line 20 all over again, correctly. When the computer notices that you have two lines numbered 20, the computer will take the bottom line 20 seriously and ignore the original line 20.

Method 2: edit Say "edit 20". That makes the computer show you line 20 again. Use the right-arrow key to move the cursor (blinking underline) to the part of the line you want to correct, then make your corrections.

To delete a character:

move the cursor to that character,

then press the DELETE key.

To replace a character:

move the cursor to that character,

then type the new character you want instead.

To insert extra characters in middle of the line:

move to where you want extra characters to begin, then press INSERT key, then type extra characters.

When you’ve finished correcting the line, press the ENTER key, which tells the computer to take the corrections seriously.

Method 3: list Say "list" (to see the whole program) or "list 20" (to see just line 20). Use the up-arrow key to move the cursor up into line 20. Then use the same editing procedure as if you used method 2. When you’ve finished correcting the line, press the ENTER key, which tells the computer to take the corrections seriously. After pressing the ENTER key, press the up-arrow or down-arrow keys to move to a different line or down to the bottom of the screen.

Fix your errors (page 333)

If you misspell CLS or PRINT, the computer will say "Syntax error" and show you the errant line. To fix the error, use one of these methods.…

Method 1: retype Press the ENTER key immediately. Then retype the errant line (and press ENTER again at the end of your retyping).

Method 2: edit Press the left-arrow key until the cursor is at the beginning of the line’s bad part. Then fix the line by retyping the bad part. Then press ENTER.

Huge & tiny numbers (page 334)

GWBASIC does not require you to use decimals for big answers. To multiply 200 by 300, you can say just "PRINT 200 * 300"; you do not need to say "PRINT 200 * 300.0".

Print on paper (page 338)

If your computer doesn’t have a PRINT SCREEN key, do a screen dump by pressing the PrtSc key while holding down the SHIFT key. (If your computer’s an IBM PC Junior, do a screen dump by pressing the Fn key, then the PrtSc key.)

WIDTH 40 (page 338)

After running a program that says WIDTH 40, you can return to a normal screen by saying:

width 80

File menu (page 339)

GWBASIC doesn’t have a file menu. Instead of using a file menu, type these commands:

Command Meaning

list Show you what program is in the RAM chips.

llist Copy the program onto paper.

save "joe" Copy the program onto your hard disk, and name the program "JOE.BAS".

files Show you a directory of all the files that are on the hard disk.

new Erase the program from the RAM chips.

load "joe" Copy the program JOE.BAS from your hard disk to the RAM chips.

run "joe" Copy the program JOE.BAS from your hard disk to the RAM chips and run it.

system Stop using GWBASIC. Make the computer say "C:\>".

So when you finish using GWBASIC, say:

system

Instant open (page 341)

Suppose you’ve saved a GWBASIC program called JOE. To use JOE, say:

C:\>gwbasic joe

That makes the computer use GWBASIC and instantly run JOE. Moreover, if JOE’s bottom line says SYSTEM, the computer will automatically exit from GWBASIC when JOE finishes running.

SLEEP (page 341)

GWBASIC doesn’t understand SLEEP. Instead of saying —

30 SLEEP

say:

30 A$ = INPUT$(1)

Instead of saying —

30 SLEEP 6

say:

30 T = TIMER

35 IF TIMER < T + 6 THEN GOTO 35

DO…LOOP (page 342)

GWBASIC doesn’t understand DO or LOOP. Replace the DO by an apostrophe, and replace the LOOP by a GOTO that says to go to the line containing the apostrophe. So instead of saying —

20 DO

30 PRINT "love"

40 LOOP

say:

20 '

30 PRINT "love"

40 GOTO 20

(If your computer’s an IBM PC Junior, here’s how to abort a program: press the Fn key, then the BREAK key.)

Line numbers (page 344)

GWBASIC does not let a line number include a decimal point.

GOTO (page 344)

GWBASIC doesn’t let you name the lines: you can’t make a line be named JOE, and you can’t say GOTO JOE. Though you can’t say GOTO JOE, you can say GOTO 10. Lines must be numbered, not named.

What’s a variable? (page 346)

When you list the program, the computer will automatically capitalize all variables: x will become X.

Long variable names (page 348)

When you list the program, the computer will automatically capitalize all variables: profit.in.1996 will become PROFIT.IN.1996.

Multi-line IF (page 354)

GWBASIC doesn’t understand multi-line IF. Use a single-line IF instead.

You can make the single-line IF be up to 255 characters long, even though the screen’s just 80 characters wide. When you reach the screen’s right margin, continue typing: the extra characters will appear underneath. Don’t press ENTER until you’ve typed the entire single-line IF.

Here’s a long single-line IF:

30 IF AGE < 18 THEN PRINT "You are still a minor": PRINT "Ah, the joys of youth"

ELSE PRINT "You are an adult": PRINT "We can have adult fun"

ELSEIF (page 354)

Since GWBASIC doesn’t understand multi-line IF, GWBASIC doesn’t understand ELSEIF.

Use a single-line IF instead. In the middle of the single-line IF, you can say ELSE IF. Put a space between the ELSE and the IF.

Here’s a single-line IF that contains ELSE IF:

30 IF AGE < 18 THEN PRINT "You're a minor" ELSE IF AGE < 100 THEN PRINT "You're

a typical adult" ELSE IF AGE < 125 THEN PRINT "You're a centenarian" ELSE PRINT

"You're a liar"

SELECT (page 355)

GWBASIC doesn’t understand SELECT. Instead, say IF and ELSE IF. For example, instead of saying —

30 SELECT CASE A$

40 CASE "fine"

50 PRINT "That's good!"

60 CASE "lousy"

70 PRINT "Too bad!"

80 CASE ELSE

90 PRINT "I feel the same way"

100 END SELECT

say:

30 IF A$ = "fine" THEN PRINT "That's good!" ELSE IF A$ = "lousy" THEN PRINT "Too bad!" ELSE PRINT "I feel the same way"

Different relations (page 357)

If you accidentally say "=<" instead of "<=", your program will still work. The GWBASIC understands and accepts "=<" and won’t bother turning it into "<=".

EXIT DO (page 359)

Since GWBASIC doesn’t understand DO, it doesn’t understand EXIT DO. Instead of saying EXIT DO, tell the computer to GOTO the line under the loop. For example, instead of saying —

30 DO

40 INPUT "What's my favorite color"; GUESS$

50 IF GUESS$ = "pink" THEN EXIT DO

60 PRINT "No, that's not my favorite color. Try again!"

70 LOOP

80 PRINT "Congratulations! You discovered my favorite color."

say:

30 '

40 INPUT "What's my favorite color"; GUESS$

50 IF GUESS$ = "pink" THEN GOTO 80

60 PRINT "No, that's not my favorite color. Try again!"

70 GOTO 30

80 PRINT "Congratulations! You discovered my favorite color."

LOOP UNTIL (page 359)

Since GWBASIC doesn’t understand DO, it doesn’t understand LOOP UNTIL. Instead of saying LOOP UNTIL, use an IF. For example, instead of saying —

30 DO

40 PRINT "You haven't guessed my favorite color yet!"

50 INPUT "What's my favorite color"; GUESS$

60 LOOP UNTIL GUESS$ = "pink"

say:

30 '

40 PRINT "You haven't guessed my favorite color yet!"

50 INPUT "What's my favorite color"; GUESS$

60 IF GUESS$ <> "pink" THEN GOTO 30

END, STOP, or SYSTEM (page 368)

Here’s how END, STOP, and SYSTEM differ from each other:

END makes the computer say "Ok".

STOP makes the computer say "Ok" but also tell you which line the
computer stopped at.

SYSTEM makes the computer say "C:\>".

Syntax errors (page 375)

If you type "prind" instead of "print", or you type extra characters at the end of a line, or you forget to type a matching parenthesis, the computer will say:

Syntax error

If you try to say PRINT 5 + 2 but forget to type the 2, the computer will say:

Missing operand

Logic errors (page 375)

If you say GOTO 10 but no line is numbered 10, the computer will say:

Undefined line number

PAUSE key (page 376)

If your computer’s an IBM PC Junior, here’s how to pause: press the Fn key, then the PAUSE key.

F keys (page 376)

GWBASIC uses the F keys completely differently:

To LIST the program, press F1 then ENTER.

To RUN the program, press F2.

To LOAD the program, press F3,

then type the program’s name, then press ENTER.

To SAVE the program, press F4,

then type the program’s name, then press ENTER.

Each line in your GWBASIC program must begin with a line number. If you type a line that does not begin with a line number, the computer will run that line immediately when you press the ENTER key at the end of the line. For example, if you say —

PRINT 4 + 2

the computer will print the answer (6) immediately when you press the ENTER key at the end of the line.

The only way to create a breakpoint is to put a STOP line into your program.

Advanced features

Bottom line (page 382) The screen’s bottom line normally contains a message reminding you that F1 is LIST, F2 is RUN, F3 is LOAD, F4 is SAVE, etc. To make that message disappear, say —

key off

or write a program containing a line saying KEY OFF. After saying KEY OFF, you can give a LOCATE command that puts info on line 25. For example, this program puts an "x" at the screen’s bottom right corner:

10 KEY OFF

20 LOCATE 25, 80: PRINT "x";

Whenever your want the bottom line’s message to reappear, say:

key on

Screen modes (page 382) If you’re using DOS 1, 1.1, 2, 2.1, or 3, you must use screen mode 0, 1, or 2. If you’re using DOS 3.1 or 3.2, you must use screen mode 0, 1, 2, or 3. If you’re using DOS 3.3 or higher, you can use any screen mode from 0 to 10. GWBASIC doesn’t understand screen modes 11, 12, or 13. Screen mode 10 works just on monochrome monitors. If you’re using DOS 3.3 or higher with an EGA or VGA color monitor, choose screen mode 9.

If your computer’s an IBM PC Junior, you can use screen modes 0, 1, and 2, plus the following special versions of modes 3, 4, 5, and 6:

PC Junior mode Pixels Colors

3 160 by 200 16

4 320 by 200 4

5 320 by 200 16

6 640 by 200 4

Modes 5 and 6 require at least 128K of RAM and require you to give this command beforehand:

clear,,,32768

You can put that command in your program.

Avoid the bottom (page 383) To avoid any problems about the screen’s bottom line, say KEY OFF at the beginning of your program. Make the top line say KEY OFF and CLS and choose a screen mode, like this:

10 KEY OFF: CLS: SCREEN 9

PLAY (page 385) If you’re using DOS 1 or 1.1, the symbols ">" and "<" don’t work.

If your computer’s an IBM PC Junior, try the following experiment.… Turn up the volume on your TV or monitor. Then say:

sound on

Then make the Junior’s three voices sing simultaneously, like this:

play "gab","efg","ccd"

While the first voice is singing "gab", the second voice sings "efg"; the third voice sings "ccd".

Printing the date & time (page 390) If you’re using DOS 1 or 1.1, your computer doesn’t understand TIMER.

RANDOMIZE TIMER (page 394) If you’re using DOS 1 or 1.1, instead of saying RANDOMIZE TIMER say RANDOMIZE VAL(RIGHT$(TIME$, 2)).

Capitals (page 400) GWBASIC doesn’t understand UCASE$ and LCASE$.

Trim (page 401) GWBASIC doesn’t understand LTRIM$ and RTRIM$.

Long integers versus short integers (page 403) GWBASIC doesn’t permit long integers. In GWBASIC, all integers are short. If you try to create an integer bigger than 32767, GWBASIC will make your number be a real number instead of an integer.

Biggest real numbers (page 403) The biggest permissible single-precision real number is 1.701411E38. The biggest permissible double-precision real number is 1.701411834604692D38.

Accuracy (page 404) GWBASIC handles a double-precision real number’s 16th digit accurately but makes slight mistakes with the 17th digit.

Tiniest decimals (page 404) The tiniest single-precision number that the computer can handle is 2.938736E-39. The tiniest double-precision number that the computer can handle is 2.938735877055719D-39. If you try to go tinier than those numbers, the computer will say zero.

Extra accuracy (page 404) If your program involves trigonometry (SIN, COS, TAN, ATN) or exponents (^, SQR, EXP, LOG), and you want those computations done with double-precision accuracy, you must say "gwbasic/d" (instead of "gwbasic") or say "basica/d" (instead of "basica").

Prime numbers (page 406) Since GWBASIC doesn’t permit long integers, omit the DEFLNG line (or say DEFINT instead of DEFLNG and use integers no bigger than 32767).

SUB procedures (page 412) GWBASIC programmers say "routine" instead of "procedure". They talk about the "main routine" and "subroutines" instead of a "main procedure" and "SUB procedures".

GWBASIC doesn’t understand the word SUB. Instead of saying —

10 CLS

20 PRINT "We all know..."

30 insult

40 PRINT "...and yet we love you."

1000 SUB insult

1010 PRINT "You are stupid!"

1020 PRINT "You are ugly!"

1030 END SUB

say this:

10 CLS

20 PRINT "We all know..."

30 GOSUB 1000

40 PRINT "...and yet we love you."

50 END

1000 PRINT "You are stupid!"

1020 PRINT "You are ugly!"

1030 RETURN

Notice that the main routine’s bottom line should say END, and the subroutine’s bottom line should say RETURN.

When you say "GOSUB 1000", the computer does a "GOTO 1000" but also remembers where it came from, so the computer can return there when you say "RETURN".

GWBASIC’s subroutines act like any other lines in the program: the subroutines do not use a separate part of the RAM. For example, if the main routine says "X = 4", the X will then be 4 in the subroutine also. If the subroutine says "X = 100", the X will then be 100 in the main routine also. All variables are automatically global and COMMON SHARED, so you don’t need to say so. Subroutines can’t have arguments or parameters.

If you say DEFINT A-Z at the beginning of the program, all variables in all routines will be short integers.

Loops (page 427) Instead of saying —

30 DO UNTIL EOF(1)

40 INPUT #1, FRIEND$

50 PRINT FRIEND$

60 LOOP

70 CLOSE

say:

30 IF EOF(1) THEN GOTO 70

40 INPUT #1, FRIEND$

50 PRINT FRIEND$

60 GOTO 30

70 CLOSE

PUT (page 428) GWBASIC doesn’t understand "STRING * 20". Instead of saying —

20 DIM RECORD AS STRING * 20

30 OPEN "jim" FOR RANDOM AS 1 LEN = LEN(record)

say:

30 OPEN "jim" AS 1 LEN=20: FIELD 1, 20 AS RECORD$

Instead of saying —

40 RECORD = "Love makes me giggle"

50 PUT 1, 7, RECORD

say:

40 LSET RECORD$ = "Love makes me giggle"

50 PUT 1, 7

Notice that line 40 begins with the word LSET, and line 50 does not end with the word RECORD.

Here’s the rule about LSET: if a variable (such as RECORD$) is in a FIELD statement, you cannot put that variable into an ordinary "=" statement; instead of saying RECORD$ = "Love makes me giggle", you must say LSET RECORD$ = "Love makes me giggle". You cannot put the variable into an INPUT statement: instead of saying INPUT RECORD$, you must say INPUT A$ and then LSET RECORD$ = A$.

In the OPEN statement, the number after "LEN=" is called the record length. If you want the record length to be more than 128 (because the string is long), you must warn the computer. For example, to warn the computer that you’ll want a record length of 200, say "gwbasic/s:200" (instead of just "gwbasic"), or say "basica/s:400" (instead of just "basica").

GET (page 428) Instead of saying —

40 GET 1, 7, RECORD

say just:

40 GET 1, 7

Multi-field records (page 428) GWBASIC doesn’t understand the word TYPE. Instead of saying —

20 TYPE COMBINATION

30 A AS STRING * 20

40 B AS STRING * 5

50 END TYPE

60 DIM RECORD AS COMBINATION

70 OPEN "jack" FOR RANDOM AS 1 LEN = LEN(record)

say:

70 OPEN "jack" AS 1 LEN=25: FIELD 1, 20 AS A$, 5 AS B$

Instead of saying —

80 RECORD.A = "I want turkey on rye"

90 RECORD.B = "yummy"

100 PUT 1, 6, RECORD

say:

80 LSET A$ = "I want turkey on rye"

90 LSET B$ = "yummy"

100 PUT 1, 6

Lengths (page 429) Saying "LEN(record)" doesn’t work well. Instead of saying "LEN(record)", just say the record length. For example, if the record length is 25, say 25. If the record length is 25, the number of records in the file is LOF(1) \ 25.

End of the file (page 429) If the record length is 25, and each record consists of part A$ and part B$, these lines print all the records:

100 FOR I = 1 TO LOF(1) \ 25

110 GET 1, I

120 PRINT A$

130 PRINT B$

140 NEXT

Numeric data (page 429) In a GWBASIC random-access file, each record must consists of strings, not numbers. If you want to store numbers in the file, you must turn the numbers into strings.

To turn an integer into a string, use the function MKI$ (MaKe from Integer). It turns the integer into a 2-byte string, even if the integer is long. For example, to turn the integer 17999 into a 2-byte string called A$, say "FIELD 1, 2 AS A$" and say "LSET A$ = MKI$(17999)".

The function MKS$ (MaKe from Single-precision real) turns a real number into a 4-byte string. The function MKD$ (MaKe from Double-precision) turns a double-precision number into an 8-byte string.

Suppose you turn a number into a string (by using MKI$, MKS$, or MKD$), and PUT the string into a file, and later GET the string back from the file. You’ll want to convert the string back to a number. Use the function CVI (ConVert to Integer) or CVS (ConVert to Single-precision) or CVD (ConVert to Double-precision). For example, if A$ is a 2-byte string that stands for an integer, you can make the computer print the integer by saying PRINT CVI(A$).

LOC (page 429) Instead of saying "PUT 1, 7" or "PUT 1, 8", you can leave the record’s number blank and say just "PUT 1". That makes the computer PUT the next record. Saying "PUT 1" has the same effect as saying "PUT 1, LOC(1) + 1". If you say "GET 1", the computer will GET the next record.

Multiple files (page 429) If you want the computer to handle two random-access files simultaneously, use two OPEN statements. In the first OPEN statement, say "AS 1"; in the second OPEN statement, say "AS 2". In the first FIELD statement, say "FIELD 1"; in the second FIELD statement, say "FIELD 2". For the second file, say 2 instead of 1 in the OPEN, FIELD, PUT and GET statements and in the LOF and LOC functions.

Error numbers (page 436) In GWBASIC, you can make these errors:

# Message

1 NEXT without FOR

2 Syntax error

3 RETURN without GOSUB

4 Out of DATA

5 Illegal function call

6 Overflow

7 Out of memory

8 Undefined line number

9 Subscript out of range

10 Duplicate Definition

11 Division by zero

12 Illegal direct

13 Type mismatch

14 Out of strong space

15 String too long

16 String formula too complex

17 Can't continue

18 Undefined user function

19 No RESUME

20 RESUME without error

22 Missing operand

23 Line buffer overflow

24 Device Timeout

25 Device Fault

26 FOR without NEXT

27 Out of Paper

29 WHILE without WEND

30 WEND without WHILE

50 FIELD overflow

51 Internal error

52 Bad file number

53 File not found

54 Bad file mode

55 File already open

57 Device I/O Error

58 File already exists

61 Disk full

62 Input past end

63 Bad record number

64 Bad filename

66 Direct statement in file

67 Too many files

68 Device Unavailable

69 Communication buffer overflow

70 Permission Denied

71 Disk not Ready

72 Disk media error

73 Advanced Feature

74 Rename across disks

75 Path/File Access Error

76 Path not found

Other computers

I’ve described five BASIC versions: QBASIC, Quick BASIC, Visual BASIC for DOS, Visual BASIC for Windows, and GWBASIC. They work on IBM-compatible computers using PC-DOS or MS-DOS.

If your computer is not IBM-compatible or does not use PC-DOS or MS-DOS, it probably uses a more primitive version of BASIC. Those primitive versions are explained in editions 1-20 of this book. For example, those editions explain versions of BASIC for the Mac, Apple 2, Commodore 64, Commodore Vic, Coleco Adam, CP/M computers, and computers made by Atari, Texas Instruments, and Timex/Sinclair. To find out which editions are still available and which ones explain your computer, phone me at 617-666-2666 anytime!