If (Expression)

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

If (Expression)
{
    Statements
}

Remarks

An If statement that contains an expression is usually differentiated from a legacy If statement such as if FoundColor != Blue by enclosing the expression in parentheses, as in if (FoundColor != "Blue"). However, this is not strictly required, as any If statement which does not match any of the legacy if patterns is assumed to contain an expression. In particular, the following are also common ways of writing an If (expression):

Known limitation: For historical reasons, If (expression) actually accepts a numeric parameter rather than a pure expression. For example, if %MyVar% is equivalent to if MyVar. This can be avoided by always enclosing the expression in parentheses.

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 with If statements that are expressions (but not legacy If statements). 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.

On a related note, If Var [not] between Low and High checks whether a variable is between two values, and If Var [not] in/contains MatchList can be used to check whether a variable's contents exist within a list of values.

Expressions, Assign expression (:=), If Var [not] in/contains MatchList, If Var [not] between Low and High, IfInString, Blocks, Else, While-loop

Examples

If A_Index is greater than 100, return.

if (A_Index > 100)
    return

If the result of A_TickCount - StartTime is greater than the result of 2*MaxTime + 100, show "Too much time has passed." and terminate the script.

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

This example is executed as follows:

  1. If Color is the word "Blue" or "White":
    1. Show "The color is one of the allowed values.".
    2. Terminate the script.
  2. Otherwise if Color is the word "Silver":
    1. Show "Silver is not an allowed color.".
    2. Stop further checks.
  3. Otherwise:
    1. Show "This color is not recognized.".
    2. Terminate the script.
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
}

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  ; Reports "0 test".