If

Specifies one or more statements to execute if an expression evaluates to true.

If (Expression)
{
    Statements
}

Remarks

If the If statement's expression evaluates to true (which is any result other than an empty string or the number 0), the line or block underneath it is executed. Otherwise, if there is a corresponding Else statement, execution jumps to the line or block underneath it.

If an If owns more than one line, those lines must be enclosed in braces (to create a block). However, if only one line belongs to an If, the braces are optional. See the examples at the bottom of this page.

The space after if is optional if the expression starts with an open-parenthesis, as in if(expression).

The One True Brace (OTB) style may optionally be used. For example:

if (x < y) {
    ; ...
}
if WinExist("Untitled - Notepad") {
    WinActivate
}
if IsDone {
    ; ...
} else {
    ; ...
}

Unlike an If statement, an Else statement supports any type of statement immediately to its right.

Related

Expressions, Assign expression (:=), Ternary operator (a?b:c), Blocks, Else, While-loop

Examples

#1

if (A_Index > 100)
    return

if (A_TickCount - StartTime > 2*MaxTime + 100)
{
    MsgBox "Too much time has passed."
    ExitApp
}

if (Color = "Blue" or Color = "White")
{
    MsgBox "The color is one of the allowed values."
    ExitApp
}
else if (Color = "Silver")
{
    MsgBox "Silver is not an allowed color."
    return
}
else
{
    MsgBox "This color is not recognized."
    ExitApp
}

#2: A single multi-statement line does not need to be enclosed in braces.

MyVar := 3
if (MyVar > 2)
    MyVar++, MyVar := MyVar - 4, MyVar .= " test"
MsgBox MyVar  ; Shows "0 test".