QBasic Tutorials: QBasic for Beginners - Chapter 3

Chapter III

How QBasic decides what to do

From the previous chapters you have learned how to create a simple program with INPUT, GOTO and PRINT commands. In such a program, you are asked to type the information, QBasic processes it and then shows the result on the screen. In many programs (for example - games), the user has a choice of what to enter. In this case, QBasic has to check what the user has typed, and to react accordingly. This can be done with the IF...THEN command.

IF…THEN…ELSE

This command checks if an argument involving a variable is true. An argument may look like this: IF a = 15 THEN... If the argument is true (and a really equals to 15), then QBasic executes the command you put after the IF...THEN.
Example:

IF a = 15 THEN PRINT "OK"

If the argument is not true (if a is not equal to 15), QBasic bypasses this line and goes to the next. In some cases, you can use the ELSE command, which tells QBasic exactly what to do if the argument is not true.

IF a = 15 THEN PRINT "OK" ELSE PRINT "It's not 15"

This example means that if a equals to 15, the computer will show OK on the screen:

OK

But if a is not equal to 15, you'll see:

It's not 15

To check the argument in IF…THEN command, you can use any of these mathematical operators:

Operator Meaning Example
= Equal to IF a = 15 THEN…
<> Not equal to IF a <> 15 THEN…
< Less than IF a < 15 THEN…
<= Less or equal to IF a <= 15 THEN…
> More than IF a > 15 THEN…
>= More or equal to IF a >= 15 THEN…

You can make QBasic to execute more than one command if the argument is true. To do this, put those commands after IF…THEN and divide them with : symbol.

IF a = 15 THEN PRINT "OK":GOTO 1

This example means that if a equals to 15, QBasic will first print OK and then will go to the line labelled 1. Here is an example of full program (a simple game):

1 CLS
score = 0
PRINT "How many days are there in a week?"
INPUT a
IF a = 7 THEN GOTO 2
PRINT "Wrong answer!"
PRINT "To try again – press 'y'."
INPUT a$
IF a$ = "y" THEN GOTO 1 ELSE END
2 score = 10
PRINT "It's the right answer!"
PRINT "Your score is now"; score; "!"
PRINT "Thanks for playing."
END

Let's analyse how this program works. The first command, CLS, clears the screen so it's empty. Then QBasic makes the variable score to be equal to 0. Then the Computer shows the question:

How many days there are in a week?

You type your answer (a number) and QBasic puts it in the variable a. Then QBasic checks if the number in this variable equals to 7 (because there are 7 days in a week). If it equals to 7, the program goes to the line 2, where the variable score gets equal to 10. You get the message:

How many days there are in a week? 7
It's the right answer! Your score is now 10! Thanks for playing.

and then the program ends. But if you gave the wrong answer (that is, the number in the variable a is not 7), QBasic bypasses the line with IF…THEN, and shows the message:

How many days there are in a week? 5
Wrong answer! To try again – press 'y'.

You can then press the key 'y' to try again or press any other key to end the game. The value of the key you pressed goes to the variable a$, which, if you remember, is a string variable (because of the $ symbol), and can contain only strings (letters, words or symbols). So the program checks if the key you pressed is really "y". If it is, the program takes you back to the line labelled 1, where the screen is cleared and the question is asked again. But if the key you pressed is some other key (not "y"), the program ends.

Sometimes you may want QBasic to execute more than two or three commands if the argument is true. Instead of putting all of them on one line, you can make an IF…THEN block:

IF a$ = "y" THEN
PRINT "OK, let's try again."
score = 0
GOTO 1
END IF

The END IF command at the end of this example. It tells QBasic that the commands, which should be executed if the argument is true, end here. This is important to separate the IF..THEN block from the rest of the program by putting END IF.

If you want QBasic to check more than one argument at once, use such words as AND and OR. For example – you want QBasic to execute commands in IF…THEN block if a is more than 12 but less than 50, somewhere in between. To program that, you can type:

IF a > 12 AND a < 50 THEN

Or, if you want commands to be executed if a equals either 6 or 12, you type:

IF a = 6 OR a = 12 THEN

Wow! So much said about that simple IF…THEN command in QBasic. It is indeed simple. IF you practise using this command in your programs, THEN you'll get the hang of it :-)