IfEqual / IfLess / IfGreater - Syntax & Usage | AutoHotkey

AutoHotkey

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

Specifies the command(s) to perform if the comparison of a variable to a value evaluates to TRUE. When more than one command is present, enclose them in a block (braces).

Deprecated: IfEqual, IfNotEqual, IfLess, IfLessOrEqual, IfGreater and IfGreaterOrEqual are not recommended for use in new scripts. See Scripting Language: If Statement for details.

IfEqual, Var, Value (same: if Var = Value)
IfNotEqual, Var, Value (same: if Var <> Value) (!= can be used in place of <>)
IfLess, Var, Value (same: if Var < Value)
IfLessOrEqual, Var, Value (same: if Var <= Value)
IfGreater, Var, Value (same: if Var > Value)
IfGreaterOrEqual, Var, Value (same: if Var >= Value)
If Var ; If Var's contents are blank or 0, it is considered false. Otherwise, it is true.

Parameters

Var

The variable name.

Value

A literal string, number, or variable reference (e.g. %var2%). Value can be omitted if you wish to compare Var to an empty string (blank).

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).

When an IF or an ELSE owns more than one line, those lines must be enclosed in braces. For example:

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

However, if only one line belongs to the IF or ELSE, the braces are optional.

Another command can only appear on the same line as the IF statement if you use the command-name style. 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 these types of if-statements. It can only be used with expression if-statements.

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

IF (expression), StringCaseSense, Assign expression (:=), if var in/contains MatchList, if var between, IfInString, Blocks, Else

Example

if counter >= 1
    Sleep, 10

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

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
    MsgBox The variable Done is neither empty nor zero.