Home Previous section Next section

The IF and THEN commands Star


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"
Output:
    x is greater than or equal to 5
    x is less than or equal to 5

ELSE

Using the ELSE command, you can have the program perform a different action if the statement is false.
    x = 3

    IF x = 5 THEN PRINT "Yes" ELSE PRINT "No"
Since X doesn't equal 5, the output is:
    No

END IF

END IF allows you to have multiple commands after the IF...THEN statement, but they must start on the line after the IF statement. END IF should appear right after the list of commands.
    x = 5

    IF (x = 5) THEN

      INPUT a$
      PRINT a$

    END IF

The following program uses ELSE with the END IF command:
    x = 16

    IF (x = 5) THEN

      INPUT a$
      PRINT a$

    ELSE

      PRINT x * 2

    END IF
Output:
    32

TIP: There is a way to have multiple commands after IF...THEN without using END IF. To do so, place a colon between each command.

IF (x = 5) THEN INPUT a$: PRINT a$



ELSEIF

The ELSEIF command allows you to perform a secondary action if the first expression was false. Unlike ELSE, this task is only performed if a specified statement is true.
    x = 6

    IF (x = 5) THEN
      PRINT "Statement 1 is true"

    ELSEIF (x = 6) THEN
      PRINT "Statement 2 is true"

    END IF
Output:
    Statement 2 is true

You can have multiple ELSEIF commands, along with ELSE.
    x = 8

    IF (x = 5) THEN
      PRINT "Statement 1 is true"

    ELSEIF (x = 6) THEN
      PRINT "Statement 2 is true"

    ELSEIF (x = 7) THEN
      PRINT "Statement 3 is true"

    ELSE
      PRINT "No above statements are true"

    END IF
Output:
    No above statements are true

Multiple expressions

You can have more than one expression in IF...THEN by using either the OR operator or the AND operator.

The OR operator only requires one expression to be true in order to print "Yes" in the following program:
    x = 20

    IF (x = 5 OR x = 20) THEN PRINT "Yes"
Output:
    Yes

The AND operator requires both expressions to be true.
    x = 7

    IF (x > 5 AND x < 10) THEN PRINT "True"
Output:
    True

This is a slightly more complex example:
    x = 16
    y = 3

    IF ((x > 5 AND x < 10) OR y = 3) THEN PRINT "Correct"
Output (since Y is 3):
    Correct

Strings in IF...THEN

So far in this chapter, we've only been dealing with numbers, but you can also use strings with the IF...THEN command.
    x$ = "Hello"

    IF (x$ = "Hello" OR x$ = "World") THEN PRINT x$
Output:
    Hello

You can also compare two variable strings:
    x$ = "Hello"
    y$ = "World"

    IF (x$ <> y$) THEN PRINT x$; " "; y$
Output:
    Hello World

Next section


Feel free to distribute this tutorial, upload it to your website, link to it from your site, etc.

http://www.geocities.com/progsharehouse/qbtutor
Mirror: http://development.freeservers.com/qbtutor