IfThen

=If/Then/Else/End If= toc

The **If** keyword signals the start of a conditional statement. There are three ways to construct a conditional **If** statement.

Syntax
IF expr THEN statement //- If expr is true, then execute statement// IF expr THEN //- If expr is true then begin an IF THEN/END IF block// ELSE //- Specify an ELSE section in an IF THEN/END IF block// END IF //- Specify the end of an IF block //

Simple If Statement
A simple conditional statement appears on a single line. It is in the form:

code If (condition) Then (action) code If the specified (condition) evaluates to TRUE, the action is taken. Here is an example.

code format="vbnet" x = 0 'simple if/then statement if x = 0 then print "Zero" code

It is permissible to use **Goto** in a conditional statement. The example below evaluate the condition, and if it is TRUE, program execution continues at the designated branch label. I this little program, since x does equal 0 and the condition evaluates to TRUE, the program execution goes directly to the [surprise] branch label. Run it and see that "No surprise." is not printed.

code format="vbnet" x = 0 'simple if/then statement if x = 0 then goto [surprise] print "No surprise." wait

[surprise] print "Surprise!" wait code

Omitting Goto
If we rewrite the example above, but omit the **Goto** statement, it works exactly the same.

code format="vbnet" x = 0 'simple if/then statement if x = 0 then [surprise] print "No surprise." wait

[surprise] print "Surprise!" wait code

Block If Statement
The second form of the **If** statement is written on multiple lines. This is useful when a block of code must be executed when a condition evaluates to TRUE. The **Block If** statement begins with a line that starts with **If**, includes one or more conditions, then ends with **Then**. It is followed by one or more lines of executable code. The final line for a **Block If** statement is **End If** A generic **Block If** looks like this:

code If (condition) Then 'code goes here print "Print something." End if code

Here is a **Block If** in action. See how it allows us to issue a **Print** statement AND a **Goto** statement if the condition evaluates to TRUE?

code format="vbnet" x = 0 'if block if x = 0 then print "Zero" goto [surprise] end if wait

[surprise] print "Surprise!" wait code

Adding Else
The **Else** keyword is optional. If it is used, it must appear on its own line, between the opening **If** statement and the closing **End If** statement. The code after the **Else** statement is executed if the condition in the **If** statement does not evaluate to TRUE. In the following example, x is equal to 2, so (x = 0) does not evaluate to TRUE. The code following the **Else** statement is executed.

code format="vbnet" x = 2 'if/then/else block if x = 0 then print "Zero" else print "Nonzero" end if code

Boolean Evaluations
We've used the = evaluator in the examples above. Other boolean expressions are possible.

``>`` Greater Than < Less Than >= Greater Than Or Equal To <= Less Than Or Equal To = Equal to

The following example uses < (less than) instead of = (equal) to evaluate the condition.

code format="vbnet" x = 3

if x < 5 then print "Less than five." end if wait code

Multiple Conditions
Multiple conditions can be evaluated in an **If** statement, using **And**, **Or**, **Xor**.

code format="vbnet" x = 3

if (x < 5) And (x>0) then print "Less than five, greater than zero." end if wait code

Not
You can also check for the condition TRUE, meaning that the expression has a value, or is nonzero. It looks like this:

code If x then print "has value" code

Here is an example. Since x is equal to 3, it has a value and the condition "if x" evaluates to TRUE.

code format="vbnet" x = 3

if x then print "x has a value." end if code

In the following example, x is equal to 0, so it has no value and the **Print** statement is not executed. code format="vbnet" x = 0

if x then print "x has a value." end if code

The use of **Not** reverses the TRUE/FALSE value. In the following code, **if not(x))** is asking, "If it is true that x has no value."

code format="vbnet" x = 0 if not(x) then print "x has no value." end if code