Keyword

# If...Then...Else

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Conditionally executes a group of statements, depending on the value of a `boolean</api/data_types/boolean>` expression.

## Usage

``` xojo
If condition Then
  [ statements ]
[ ElseIf condition-n Then
  elseIfStatements ... ]
[ Else
  elseStatements ]
End [ If ]
```

**or**

``` xojo
If condition Then statement [ Else statement ]
```

| Part             | Description                                                                                                                                                                           |
|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| condition        | Required. A `boolean</api/data_types/boolean>`, numeric, or `string</api/data_types/string>` expression that evaluates to `True</api/language/true>` or `False</api/language/false>`. |
| statements       | Optional. One or more statements that are executed if *condition* is `True</api/language/true>`.                                                                                      |
| condition-n      | Optional. Same as *condition*.                                                                                                                                                        |
| elseIfStatements | Optional. One or more statements executed that are executed if the associated *condition-n* is `True</api/language/true>`.                                                            |
| elseStatements   | Optional. One or more statements executed if no previous *condition* or *condition-n* expression is `True</api/language/true>`.                                                       |

## Notes

For conditional compilation, see `＃If...＃EndIf</api/compiler_directives/if...endif>`.

When executing an If statement, the condition is tested. If *condition* is `True</api/language/true>`, the statements associated with the If statement following the Then statement are executed. If *condition* is `False</api/language/false>` and an Else clause follows, its statements will be executed. If *condition* is `False</api/language/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</api/language/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 Return` (`⌘ ⇧ Return` on macOS). The Code Editor will then add the Then and the End If and put the cursor between them.

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:

``` xojo
If error = 123 Then MessageBox("An error occurred.")
If error = 123 Then MessageBox("An error occurred.") Else MessageBox("Never mind!")
```

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

``` xojo
If error = 123 Then System.Beep MessageBox("An error occurred.") ' Syntax error
```

You can declare variables using the `Var</api/language/var>` statement inside an If statement. However, such variables go out of scope after the End If statement. For example:

``` xojo
If error = -123 Then
  Var a As String
  a = "Whoops! An error occured."
End If
MessageBox(a) ' Error: This method or property does not exist because a has gone out of scope
```

## Sample code

This example shows a simple If statement:

``` xojo
If error = -123 Then
  System.Beep
  MessageBox("Whoops! An error occured.")
End If
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

``` xojo
Var theNumber As Integer
Var digits As Integer
theNumber = 33
If theNumber < 10 Then
  digits = 1
ElseIf theNumber < 100 Then
  digits = 2
ElseIf theNumber < 1000 Then
  digits = 3
Else
  digits = 4
End If
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This example demonstrates using And to combine multiple conditions:

``` xojo
Var theNumber As Integer = 50

If theNumber > 10 And theNumber < 60 Then
  MessageBox("In range")
Else
  MessageBox("Out of range")
End If
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

By default, this code will display "In Range" because 50 is between 10 and 60.

This example uses Or to combine multiple conditions:

``` xojo
Var theNumber As Integer = 5

If theNumber <= 10 Or theNumber >= 60 Then
  MessageBox("Out of range")
Else
  MessageBox("In range")
End If
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Select Case</api/language/select_case>` statement, `If</api/language/if>` operator, `＃If...＃EndIf</api/compiler_directives/if...endif>`

</div>
