Else

AutoHotkey

Else

Specifies the command(s) to perform if an IF-statement evaluates to FALSE. When more than one command is present, enclose them in a block (braces).

Else

Remarks

Every use of ELSE must belong to (be associated with) an IF-statement above it. An ELSE always belongs to the nearest unclaimed IF-statement above it unless a block is used to change that behavior.

An ELSE can be followed immediately by any other single command on the same line. This is most often used for "else if" ladders (see examples at the bottom).

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

if count > 0  ; No braces are required around the next line because it's only a single line.
    MsgBox Press OK to begin the process.
else  ; Braces must be used around the section below because it consists of more than one line.
{
    WinClose Untitled - Notepad
    MsgBox There are no items present.
}

The One True Brace (OTB) style may optionally be used around an "else". For example:

if IsDone {
    ...
} else if (x < y) {
    ...
} else {
    ...
}

Related

See Blocks. Also, every IF-command can use ELSE, including IfWinActive, IfWinExist, IfMsgBox, IfInString, IfBetween, IfIn, IF, and IF (expression).

Examples

IfWinExist, Untitled - Notepad
{
    WinActivate
    Send This is a test.{Enter}
}
else
{
    WinActivate, Some Other Window
    MouseClick, left, 100, 200
}

if x = 1
    Gosub, a1
else if x = 2 ; "else if" style
    Gosub, a2
else IfEqual, x, 3 ; alternate style
{
    Gosub, a3
    Sleep, 1
}
else Gosub, a4  ; i.e. Any single command can be on the same line with an ELSE.
 
; Also OK:
IfEqual, y, 1, Gosub, b1
else {
    Sleep, 1
    Gosub, b2
}