Beginner's Programming Tutorial in QBasic

(Kiana) #1

The IF and THEN commands


The IF and THEN commands are used to compare an expression and then perform some task
based on that expression.
x = 5
IF x = 5 THEN PRINT "x equals 5"
Since X does equal 5 in this case, the program outputs:
x equals 5


Expression signs


You can also enter the following statements, instead of the equals sign:
x < 5 (x is less than 5)
x > 5 (x is greater than 5)


Run the following:
x = 16
IF (x > 5) THEN PRINT "x is greater than 5"
Output:
x is greater than 5


You can also combine the signs like this:
x <= 5 (x is less than or equal to 5)
x >= 5 (x is greater than or equal to 5)
x <> 5 (x does not equal 5)


Run the following example:
CLS
x = 5
IF (x >= 5) THEN PRINT "x is greater than or equal to 5"IF (x <= 5) THEN PRINT "x is less than or equal to 5"
IF (x <> 5) THEN PRINT "x does not equal 5"

Free download pdf