if (expression)

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

if (expression)

Remarks

An if-statement that contains an expression is usually differentiated from a traditional-if such as If FoundColor <> Blue by enclosing the expression in parentheses. 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, 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 with if-statements that are expressions (but not traditional if-statements). For example:

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

Unlike an "else" statement -- which supports any type of statement immediately to its right -- an if-statement supports only a "{" to its right.

On a related note, the command if var [not] between LowerBound and UpperBound checks whether a variable is between two values, and if var [not] in value1,value2 can be used to check whether a variable's contents exist within a list of values.

Related

Expressions, Assign expression (:=), if var in/contains MatchList, if var between, IfInString, 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
}