If...Then


If Condition Then ...

If the Condition (Boolean Expression) is true then the statement after the THEN is executed. If the Condition is false, the execution continues on the next line.

For example:

A = 5
If A < 10 Then Print "Hello"


Since A is equal to 5 it is less than 10. The condition is true so "Hello" will be printed.

The following are a list of valid operators that may be used in a Condition (Boolean expression).

Comparison operators

<=

(less than or equal)

<>

(not equal to)

>=

(greater than or equal)

=

(equal)

>

(greater than)

<

(less than)

and

(logical and)

or

(logical or)

not

(logical not)

( )

(parenthesis)


And & Or

Compound Conditions can be created using the And & Or Operators.

A = 5
B = 6
If A < 10 And B > 5 Then Print "Hello"

In this Condition two values are being compared. Since an And is used, both Conditions must be true for "Hello" to be printed.

A = 5
B = 6
If A < 10 Or B > 7 Then Print "Hello"

Here, with an OR, two values are also being compared. Since an Or is used, either the first Condition or the second Condition or both must be true for "Hello" to be printed.

If a condition is just a numeric expression with no comparison operators, as in:

A = 0
If A Then Print "Hello"

The condition is false if the expression is zero. In the example above A = 0, so "Hello" will not be printed. Any non-zero value will be considered true.

A THEN may not be followed by a GOTO, FOR, NEXT or another IF.


Not

Intuitively, a Not operator returns true when a condition is false, and false when a condition is true.

If Not isFile("Test.txt") Then Gosub CreateFile

If Not isDir("Temp") Then Gosub CreateTempDir

If Not isNumber("12s5") Then Gosub DisplayError


If...Then...Endif


The second form is the compound If...Then...Endif structure. It can be written as follows.


a = 5
If a < 10 Then
...
ElseIf a > 20 Then
...
Else
...
Endif


In a Compound If...Then structure, the "Then" must end the line. Nothing may follow the "Then", not even a comment.

Compound If...Then's may be nested.

A compound If may have more than one ElseIf or none.

The Else statement is optional. If included, it must be placed last before the EndIf.

It is not recommended to exit a compound If...Then with a Goto. Doing so will not cause an error, but the interpreter's ability to detect unmatched Else's and Endif's will be compromised.






Become a Patron