If / IfEqual / IfNotEqual / IfLess / IfLessOrEqual / IfGreater / IfGreaterOrEqual

Specifies one or more statements to execute if the comparison of a variable to a value evaluates to true.

Deprecated: Legacy If statements are not recommended for use in new scripts. See Scripting Language: If Statement for details and use If (expression) instead.

IfEqual, Var , Value          ; if Var = Value
IfNotEqual, Var , Value       ; if Var != Value
IfLess, Var , Value           ; if Var < Value
IfLessOrEqual, Var , Value    ; if Var <= Value
IfGreater, Var , Value        ; if Var > Value
IfGreaterOrEqual, Var , Value ; if Var >= Value

Parameters

Var
The name of a variable. Percent signs must be omitted except when attempting a double reference. Unlike the input variables of other commands, the percent prefix is not supported.
Value
If blank or omitted, Var will be compared to an empty string. Otherwise, specify an unquoted text or a number. Variable references must be enclosed in percent signs (e.g. %var2%).

Remarks

If both Var and Value are purely numeric, they will be compared as numbers rather than as strings. Otherwise, they will be compared alphabetically as strings (that is, alphabetical order will determine whether Var is greater, equal, or less than Value).

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. For example:

if count <= 0
{
    WinClose Untitled - Notepad
    MsgBox There are no items present.
}

Note that command-like If statements allow a command or command-like control flow statement to be written on the same line, but mispelled command names are treated as literal text. In other words, these are valid:

IfEqual, x, 1, Sleep, 1
IfGreater, x, 1, EnvAdd, x, 2

But these are not valid:

if x = 1 Sleep 1
IfGreater, x, 1, x += 2

The One True Brace (OTB) style may not be used with legacy If statements. It can only be used with If (expression).

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.

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

Examples

If counter is greater than or equal to 1, sleep for 10 ms.

if counter >= 1
    Sleep, 10

If counter is greater than or equal to 1, close Notepad and sleep for 10 ms.

if counter >= 1   ; For executing more than one line, enclose those lines in braces:
{
    WinClose, Untitled - Notepad
    Sleep 10
}

This example is executed as follows:

  1. If MyVar is equal to MyVar2, show "The contents of MyVar and MyVar2 are identical."
  2. Otherwise if MyVar is empty:
    1. Show "MyVar is empty/blank. Continue?" and wait for user input.
    2. If the user presses "No", stop further checks.
  3. Otherwise if MyVar is not a comma, show "The value in MyVar is not a comma.".
  4. Otherwise show "The value in MyVar is a comma.".
if MyVar = %MyVar2%
    MsgBox The contents of MyVar and MyVar2 are identical.
else if MyVar =
{
    MsgBox, 4,, MyVar is empty/blank. Continue?
    IfMsgBox, No
        Return
}
else if MyVar != ,
    MsgBox The value in MyVar is not a comma.
else
    MsgBox The value in MyVar is a comma.

If Done is neither empty nor zero, show "The variable Done is neither empty nor zero.".

if Done
    MsgBox The variable Done is neither empty nor zero.