IF-THEN-ELSE Statements

IF-THEN-ELSE statements are the one of the most basic control structures in QBasic. The general form of an IF statement is IF (some condition is true) THEN (do something) ELSE (do something else). So, if the condition is true, then some piece of code is executed, if the condition is not met then, perform some other piece of code, the ELSE clause is completely optional.
The discussion of IF statements brings us to relational operators, which are operators that evaluate to either true of false. relational operators are sometimes called 'boolean expressions' which just means that there are only two possible results. These relational operators are, in QBasic, ( <, >, =, <>, >=, <=, ). Which are used just as they are read, less than, greather than, equals ect...
Okay, back to IF-THEN statements, so here's some examples that should clear things up:

� � � � A% = 5
� � � � IF A% > 0 THEN PRINT "A% = "; A% ELSE PRINT "A% is 0"

Output:

A% = 5

Example2:

� � � � INPUT "What is Your Name"; name$
� � � � IF name$ = "Nable" THEN PRINT "Your Name is Nable."
� � � � IF name$ = "Bill" THEN PRINT "Your Name is Bill."

Output:

What is your Name? Nable
Your Name is Nable.

One note on this exampe, if you did not input either 'Bill' or 'Nable' then this code will do nothing, the same thing goes for any conditional statement where no conditions are met.
Okay, now that you've got the general Idea of how an IF-THEN statement works, we can do something a little more complicated; AND and OR statements. AND and OR are examples of 'logical operators' which, in this case, just allow you to test for more than one condition in a given statement. There are 6 logical operators in QBasic, however their use is more complicated than this tutorial will cover, so they will be ommited, if you are that interested in what they are and do you can find them in the help file. So for now we'll stick to our discussion of AND and OR, here is an example:

� � � � A% = 10
� � � � IF A% <= 5 OR A% >= 10 THEN PRINT "The value of A% is: "; A%
� � � � IF A% >= 5 AND A% <= 50 THEN PRINT "The value of A% is still: "; A%

Output:
� � � � The Value of A% is: 10
� � � � The Value of A% is still: 10

In this example there is only one AND or OR statement per IF stetement, but it is possible and often necessary to use multiple AND and OR operators in a given IF statement.
� � � � There are some times when you will find it necessary to execute more than one line of code as a result of some condition. There are, generally, two ways to do this, the first is inline, where you just place a colon after each operation you perform.
Example:

� � � � A% = 1
� � � � IF A% > 0 THEN PRINT "A% is greater than 0": END

Output:
� � � � A% is greater than 0

The next way of performing more than one line of code per IF statement is called the IF block. The Form of an IF block looks like this:
� IF condition THEN
� � 'Some code goes here
� END IF
This is just like a normal IF statement, except that if you put anything on the same line as, and after, the THEN, you will get an error, you also have to remember to place the END IF at the end of the code that you wish to execute.
Example:

� � � � A% = 5
� � � � IF A% = 5 THEN
� � � � � PRINT "A% is 5"
� � � � � END
� � � � END IF

Output:
� � � � A% is 5

Also, there is a way to use the ELSE clause with the IF block. The syntax is similar to that of the IF block, except that instead of placing an END IF at the end of the block, you put an ELSEIF at the end of the block, and then an END IF at the end of that block. The ELSEIF statements can be reapeated as many times as you like.
Here is an example:

� � � � A% = 10
� � � � IF A% = 5 THEN
� � � � � PRINT "A% is 5"
� � � � � END
� � � � ELSEIF A% = 10 THEN
� � � � � PRINT "A% is 10"
� � � � � END
� � � � END IF

Output:
� � � � A% is 10

This is method of selecting specific bits of code to execute is nice, but with large programs it can get messy. If you want to select a specific piece of code to run, then you'll have to use one of these blocks every time you want to run that code. However, you'll get tired of typing the same thing over and over again, so there is are eaiser ways of doing it, one way is by using GOTO's which is the topic of the next section.

� � Back � � � � Tutorial Part 1 � � � � Next � �