Else

Specifies one or more statements to execute if an If statement evaluates to false.

Else Statement
Else
{
    Statements
}

Remarks

Every use of an 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 statement on the same line. This is most often used for "else if" ladders (see examples at the bottom).

If an Else owns more than one line, those lines must be enclosed in braces (to create a block). However, if only one line belongs to an 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 {
    ; ...
}

Blocks, If Statements, Control Flow Statements

Examples

Common usage of an Else statement. This example is executed as follows:

  1. If Notepad exists:
    1. Activate it
    2. Send the string "This is a test." followed by Enter.
  2. Otherwise (that is, if Notepad does not exist):
    1. Activate another window
    2. Left-click at the coordinates 100, 200
if WinExist("Untitled - Notepad")
{
    WinActivate
    Send This is a test.{Enter}
}
else
{
    WinActivate, Some Other Window
    MouseClick, Left, 100, 200
}

Demonstrates different styles of how the Else statement can be used too. Note that IfEqual is deprecated and should generally be avoided.

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 statement can be on the same line with an Else.
 
; Also OK:
IfEqual, y, 1, Gosub, b1
else {
    Sleep, 1
    Gosub, b2
}