BASIC: Beginner's All-Purpose Symbolic Instruction Code: What Does BASIC Look Like A Larger Program: Animal - BAS
BASIC: Beginner's All-Purpose Symbolic Instruction Code: What Does BASIC Look Like A Larger Program: Animal - BAS
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.
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.)
create a new folder, such as C:\mybasic start an MS-DOS command window cd C:\mybasic type the program as shown into NOTEPAD and save it as HELLO.BAS. notepad hello.bas type the command 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.
180 190 200 210 220 230 240 250 260 270
PRINT name$;", do you know how much ";num1;" plus ";num2" add up to?" PRINT "Please type the sum here"; INPUT num3 IF (num3 = (num1 + num2)) then PRINT "Very good, ";name$;", that is correct!!" ELSE PRINT "I'm sorry, that is not correct." PRINT num1, "plus", num2, "is", num3 END IF END
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.
"list" shows the "decision tree" that the program has built so far. "save" saves the decision tree to the file "animal.dat" "load" loads a saved decision tree into the program "quit" ends the program "yes" starts the guessing - anything else repeats the question.
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 is what gets written when you tell the program to "save" after playing a couple of rounds. See how each line contains:
The line number "Q" for a question or "A" for an answer The text of the question (or the name of the animal) The line number to go to if it is a "yes" The line number to go to for a "no"
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.
Use the correct article (a or an) in front of the animal name when asking the user. Strip off the article (a or an) if the user enters it when adding a new animal. Be more forgiving about answers: Allow not only "yes", but also "YES", "Y", "yessir", "okay" etc. Ask again if you can't recognize the answer as a yes or a no.
You could also expand the scope beyond animals to a full "20 questions" game.