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:
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
Here is a more complex program:
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.
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.
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".
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.
A Slightly More Interesting 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.
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.
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.
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.
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