if (expression)

AutoHotkey

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 differentiated from a traditional-if such as If FoundColor <> Blue by making the character after the word "if" an open-parenthesis. Although this is usually accomplished by enclosing the entire expression in parentheses, it can also be done with something like if (x > 0) and (y > 0). In addition, the open-parenthesis may be omitted entirely if the first item after the word "if" is a function call or an operator such as "not" or "!".

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
}