From Xojo Documentation

Revision as of 19:00, 19 November 2009 by WikiSysop (talk) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!


Conditionally executes a group of statements, depending on the value of a boolean expression.


If condition Then'statements[ElseIf condition-n Then'elseIfStatements]... ['Else'elseStatements] End [If] OR 'If condition Then 'statement' [Else] ['statement]

Part Description
condition Required. A boolean, numeric, or string expression that evaluates to True or False.
statements Optional. One or more statements that are executed if condition is True.
condition-n Optional. Same as condition.
elseIfStatements Optional. One or more statements executed that are executed if the associated condition-n is True.
elseStatements Optional. One or more statements executed if no previous condition or condition-n expression is True.


When executing an If statement, the condition is tested. If condition is True, the statements associated with the If statement following the Then statement are executed. If condition is False and an Else clause follows, its statements will be executed. If condition is False and there is no Else clause or it is preceded by an ElseIf statement, the condition following the ElseIf statement is tested. After executing the statements following Then, ElseIf or Else execution continues with the statement that follows End If.

When writing an If statement, you can use a couple of shortcuts. First, you can write only the statements that are executed if the condition is True. Then select the statements, display the Code Editor's contextual menu, and choose Wrap in If...End if. The Code Editor will then add an If statement above the selected lines and an End If statement below them. All you need to do is write the condition.

The other shortcut is this: Write the "If condition" portion of the statement and then press Ctrl+Shift+Enter. The Code Editor will then add the "Then" and the "End if" line.

An If statement can be written on one line, provided the code that follows the Then and Else statements can be written on one line. Using this syntax, you omit the End if statement. For example, the following examples are valid:

If error=123 Then MsgBox "An error occured."
If error=123 Then MsgBox "An error occured." Else MsgBox "Never mind!"

The following is not valid because the Then clause requires two lines:

If error=123 Then Beep MsgBox "An error occured."

Use the syntax shown in the first example.

You can declare variables using the Dim statement inside an If statement. However, such variables go out of scope after the End If statement. For example

If error=-123 Then
Dim a As String
a="Whoops! An error occured."
End if
MsgBox a //out of scope


This example shows an If statement.

If error=-123 Then
MsgBox "Whoops! An error occured."
End If

This example shows an If statement that includes the use of ElseIf and Else clauses.

Dim theNumber As Integer
Dim digits As Integer
If theNumber<10 Then
ElseIf theNumber<100 Then
ElseIf theNumber<1000 Then
End If

See Also

Select Case statement.