This article is dedicated solely to those who have never programmed before or know next to nothing about programming in QBasic. Starting here, the QB Cult magazine begins a step-by-step series that explains, from the beginning all you need to know to be able to create your own top quality programs successfully.
Many people find that after a while, the pre packaged programs and games that they've bought for their computer start to become a little boring and they begin to wonder if they can modify them or even write their own. But a computer can do nothing by itself. It must be given a list of instructions telling it in minute detail exactly what to do and how to go about achieving it. These instructions form what is called a program and the art and science of creating them is called programming.
There is nothing difficult about programming. You dont even have to be good at maths, unless of course, you want to write programs to perform mathematical tasks. All you need to begin with is to understand BASIC.
In order to write programs you are going to have to get a version of BASIC. As this magazine is dedicated to QuickBASIC users, you are going to have to download it, if you dont already have it. If you have a WINDOWS 95 CD then go to the directory OLDMSDOS and there you will find two files: QBASIC.EXE and QBASIC.HLP. Copy these files onto your hard drive, and then double click on the application QBASIC. You are now ready to write your first program in BASIC!
Let us begin by writing a small program and seeing what happens. This one will ask you to enter a number. It will then add one to that number and display the result. Type in the following program exactly as it is shown here, and once that has been done, push the F5 key to run the program.
REM Simple addition program CLS PRINT "Type in a number and push the ENTER key" INPUT n n = n + 1 PRINT "The number that you typed plus 1 is"; n
Let us examine what is actually going on in the above program. The first line of code uses the REM statement. REM stands for REMark. It is used to allow explanatory remarks to be inserted in a program. REM statements are not executed. Once a REM or its abbreviation, an apostrophe (') is encountered, the program ignores everything else until the next line of code.
The second line of code uses the CLS command. This stands for 'CLear Screen' and removes everything currently being displayed on the screen.
Line three uses a very usefull command called PRINT. This command outputs text to the screen. Everything that is in the inverted commas " " will be displayed. For example, if you had to type PRINT "Hello world" and run the program, Hello world would be displayed on the screen.
Line four uses the INPUT command. It allows you to enter something into a program. It is followed by what is called a variable. The variable is the name of the place in the computers memory where the information that you typed will be stored. The enter key must be pushed after the information has been entered, as seen in the simple addition program above. Please note that you may call the variable anything you like.
Line five tells the computer to add one to the variable named n.
The last line of code once again uses the PRINT command, however, in this case it is used far more cleverly. It displays a message and then displays the value of the variable n. This is done by the adding of a ; sign after the last " sign, and then writing the name of the variable whose contents you wish to display.
I will now give you another example. This one is quite simular to the above with the exception that the variables will be used to store letters and words instead of numbers. To achieve this, a dollar sign $ will be added to the end of the name of the variable, for example: W$ In adding a $ sign to the end of the name of the variable, you tell the computer that you wish to store letters/text in the variable instead of numbers. These kinds of variables that store text are called string variables. This program also demonstrates that you can use several letters to form the name of a variable. Type out the following program exactly as shown here, and then run it by pushing the F5 key.
REM This program asks for your name and displays it CLS PRINT "Enter your name and then press the ENTER key" INPUT NAME$ PRINT "Hello"; NAME$; "how are you doing?"
Okay, now you have learned how to work with variables, and how to display the values they contain. However, so far we have only been able to add and subtract values from the numeric variables, what if we want to multiply and divide numbers? Well, lets see if we can answer that question by means of an example. We will also use INPUT in a slightly better way. Type out the following program exactly as it is seen here, and then push the F5 key when done to see how it works.
REM Multiplying and dividing numbers in Qbasic CLS INPUT "Enter your first number and then press the ENTER key"; Number1 INPUT "Enter your second number and then press the ENTER key"; Number2 PRINT "Number 1 multiplied by number 2 = "; Number1 * Number2 PRINT "Number 1 divided by number 2 = "; Number1 / Number2
Well, that brings us to the end of part one in the series of this helpfull section for beginners. With what you have learned here you can easily create some nice text adventure games, or even some simple business software. Next month we will be looking at loop structures and graphics.
Happy programming! (is there any other kind?)