BASIC: Beginner's All-purpose Symbolic Instruction Code

Introduction

Hundreds of thousands of computer professionals have had their first introduction to computer programming by way of the BASIC language. It is not necessarily the best language for teaching beginners, but it does have a couple of things to recommend itself. Among them, the fact that it is available freely on Windows in the form of Microsoft QBASIC, which is still included on Windows98 Second Edition, although it is hidden away in \WIN98\TOOLS\OLDMSDOS. Just copy QBASIC.EXE to C:\WINDOWS and you are in business. (But you would be well advised to also take HELP.EXE, HELP.HLP and QBASIC.HLP). In the following, I am assuming that you are running QBASIC under MS Windows.

What Does Basic Look Like?

It is traditional to illustrate the flavor of a programming language by showing a small program that displays the words "Hello World!". In BASIC, that could look like this:
100 REM This is a sample BASIC program
110 PRINT "HELLO WORLD!"
120 SYSTEM

But before we get too far ahead, let me scale down your expectations a bit. BASIC was invented and in widespread use before computers had windows, in fact before they had display screens. The world in which BASIC came to life, was one in which only one program at a time was running on the computer, and it used a teletype terminal (a kind of electric typewriter) to talk to the user. In some cases, the terminal was connected to a large computer via a modem and a telephone line, but each user saw BASIC as a small computer that he alone used. QBASIC runs in an MS-DOS command window and tries to make windows look like that nostalgic vision. There are newer and fancier BASIC versions for windows (called Visual Basic) that will draw real windows with dialog boxes and buttons, but we are trying to keep things simple for now, so the restrictions suit us just fine.

The BASIC instructions in this program are:

100 REM This is a simple BASIC program
REM is short for remark, what other programming languages call a comment. It does not do anything, but allows you to put an explanation into the program, for the benefit of someone reading the program later. (Which could be yourself. It is remarkable, how cryptic your program can seem 6 months later, when you want to make a small change.)
110 PRINT "HELLO WORLD!"
PRINT writes something on the screen. The things to write are put between quote marks to distinguish them from the program.
120 SYSTEM
SYSTEM is not standard BASIC, but is part of QBASIC. It means to leave the program and go back to the operating system (DOS or WINDOWS). In other versions of BASIC, the preferred word is STOP, EXIT or END. In QBASIC, STOP brings up the QBASIC program editor, even if you did not start the program from there. In QBASIC, END works somewhere in between SYSTEM and STOP. (In some other versions of BASIC, everything after END is thrown away.)

How do I run QBASIC

There are several ways to run QBASIC. The first and simplest would be to and then from the MS-DOS command line type
QBASIC /RUN HELLO

If you typed the program correctly, you would see the message and a new C:\mybasic> prompt.

The next step would be to just type QBASIC. This starts QBASIC and gives you the choice between seeing a help file and starting to work in QBASIC. This time, take the latter option, by hitting the ESC key (upper left corner of your keyboard). This puts you on a blue window where you can type/edit programs. Now click on File in the upper left corner of the window, select Open and click on HELLO.BAS and click on . This brings up the program on your blue window. You can now change it. Click on the W in WORLD and type your first name, the word "and" and spaces on both sides of "and". Then click on Run and Start at the menu bar on top of the screen.

A Slightly More Interesting Program

Here is a more complex program:

100 REM This program reads and writes
110 PRINT "What is your name";
120 INPUT name$
130 PRINT "Hi, ";name$;"!"
140 PRINT "Please type a number";
150 INPUT num1
160 PRINT "Please type another number";
170 INPUT num2
180 PRINT name$;", do you know how much ";num1;" plus ";num2" add up to?"
190 PRINT "Please type the sum here";
200 INPUT num3
210 IF (num3 = (num1 + num2)) then
220 PRINT "Very good, ";name$;", that is correct!!"
230 ELSE
240 PRINT "I'm sorry, that is not correct."
250 PRINT num1, "plus", num2, "is", num3
260 END IF
270 END
This program introduces the concepts of Let's look at each of these in turn.

Variables: Holding a value

In order to do useful things, programs need to work on information. A variable is a memory cell that has a name and can hold a value. In basic, we have three kinds of values: A general number is just what it sounds like. These variables can have values such as 27, 274,327, -6 or 3.14159; their names are words.

A whole number cannot have a decimal point. If you try to stick a decimal fraction into it, it gets rounded off to the nearest whole number. Their names end with a percent sign.

A string is any sequence of symbols, although you will have to jump through hoops to get some punctuation marks into them. Their names end with a dollar sign.

In the example above, name$ is a string, while num1, num2 and num3 are general numbers.

Input

In BASIC, the INPUT command puts out a question mark and then reads from the keyboard. If more than one variable is being read, the values are separated by commas. This works really well, but makes it hard to read commas. The input ends with typing the "ENTER" key.

Decisions

The IF statement allows you to make decisions and choose between different things to do, depending on the values of variables. Between the words IF and THEN is a calculated value that must be true or false. If it is true, the first group of statements is executed (from THEN to ELSE). If it is false, the second group is executed (from ELSE to END IF). Sometimes, there is no need for an ELSE, and you can use the simpler form
	IF (test) THEN
	...
	END IF

Control of Output Style

You can print more than one thing with a single PRINT statement. If you have several items to print, you can separate them with either commas or semicolons.

If you choose commas, BASIC will choose the way they are printed, line them up in columns, make sure there is some space between them and switch to a new line when the line is full.

If you choose semicolons, BASIC will print exactly what is in the variable, and it is up to you to make sure there is space between the items, or that the lines do not get too long. If the PRINT statement ends with a simucolon, the cursor is left at the end of the line, and the next PRINT or INPUT command will start there.

A Larger Program: Animal.BAS

The program ANIMAL.BAS is a great program to play with. As it stands, it is working but just a little too picky about how you work with it. Click on the link above, then save the program in C:\mybasic\animal.bas, and play with it.

The program will ask you to think of an animal, and ask "Are you ready?" You must answer "yes" to continue. Let us assume that you are thinking of a mosquito.

The program asks "Is it a elephant". For now, let's not worry about the syntax error (should really be an elephant) and just say "no". Since elephants are all that the program knows about, it will say "I give up, what is it?" and you should answer "mosquito" (without any "a" or "an" in front).

The program goes on to ask "What is a question to distinguish between a elephant and a mosquito ?" to which you might answer "Is it an insect" (don't type the question mark). Program asks "For a elephant, the answer is?" and you answer "no".

The program now starts over, but instead of asking "Is it a elephant" it will now start by asking "Is it an insect?"

In this way, you can teach the program the whole animal kingdom.

When the program asks "Are you ready?" you can answer several things:

For all yes/no questions, a "yes" must be exactly three letters in lowercase. Anything else (including "y" and "Yes") is taken as "no".

More BASIC: Understanding the ANIMAL.BAS Program

(When you read the following, it is helpful to look at the program. You can either print it out, or open a second window and flip back and forth. But a printout will allow you to write notes on the paper.)

Data Structures

The first thing to note about this program, is that most of the smarts are in the structure of the information that the program works with. Here is a sample:
 1 ,Q,Is it an insect, 3 , 2 
 2 ,Q,Does it live in the water, 5 , 4 
 3 ,Q,Is it a parasite, 8 , 9 
 4 ,Q,Is it a mammal, 6 , 7 
 5 ,A,shrimp, 0 , 0 
 6 ,Q,Is it a carnivore, 11 , 10 
 7 ,A,condor, 0 , 0 
 8 ,A,flea, 0 , 0 
 9 ,A,fruitfly, 0 , 0 
 10 ,A,elephant, 0 , 0 
 11 ,A,dog, 0 , 0 
This is what gets written when you tell the program to "save" after playing a couple of rounds. See how each line contains: Most of the work in writing the program was in deciding that this is the information needed to do the job.

The Program

The program is still here.

This program uses a new concept, the array. An array is a table of variables, where the entries in the table can be addressed by a whole number (from 1 and up to some maximum size of the table). An array in BASIC is created by a DIM statement (short for dimension). In this program, we have an array for each of the elements on the line described above, and we use the line number as the index into the array. The nodetype (Q/A) is represented by a 0 for a question and 1 for an answer.

The first section deals with the commands "load", "save" and "quit".

The next section works its way through the existing table of questions, until it gets to an animal to ask about.

The last section deals with adding another question after the user answered "no" to the proposed animal.

Improving the Program

This program is quite simple, and can be improved in a numebr of ways: You could also expand the scope beyond animals to a full "20 questions" game.

Other Programs You Might Write

A program to read a program and write it out again with new line numbers (100, 110, 120 etc). This is very useful after you have worked on a section of the program for a while and used up all the numbers in that area, leaving no room for a new line you want to insert.



Copyright © 2000 Lars Poulsen, All Rights Reserved
These pages may be freely copied in their entirity only.
For other uses, please contact the author.