if (expression)

Auto Hotkey

if (expression)

Specifies the command(s) to perform if an expression evaluates to TRUE.

if (expression)

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, execution jumps to the line or block underneath it.

When an IF or an ELSE owns more than one line, those lines must be enclosed in braces. However, if only one line belongs to an IF or ELSE, 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 {
    ...
}

Any type of statement can appear immediately to the right of an if-statement, but unlike an else-statement, a delimiting comma is required. For example:

if true, MsgBox  ; Good
if true  MsgBox  ; Bad

Related

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

Example

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
}