Operator

# If

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

## Description

A ternary operator that evaluates an expression and returns results when it is `True</api/language/true>` or `False</api/language/false>`.

## Usage

``` xojo
If(condition, <result when expression is true>, <result when expression is false>)
```

| 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>`. |
| result when expression is true  | Required. This result is returned when the expression evaluates to `True</api/language/true>`.                                                                                        |
| result when expression is false | Required. This result is returned when the expression evaluates to `False</api/language/false>`.                                                                                      |

## Notes

The return type is the common type between the two result values. For example, <span class="title-ref">If</span> an Int8 and an Int32 are used as result values, the result type will actually be Int32. Having no common type results in a `Type Mismatch</api/errors>` compile error.

This operator uses the same evaluation rules as a normal `If...Then...Else</api/language/if...then...else>` statement. Only the result that is needed gets evaluated.

## Sample code

This code outputs "Big number":

``` xojo
Var myInteger As Integer = 42
MessageBox(If(myInteger > 40, "Big number", "Small number"))
```

## Compatibility

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

<div class="seealso">

`If...Then...Else</api/language/if...then...else>`, `#If...#EndIf</api/compiler_directives/if...endif>`

</div>
