Else

Specifies one or more statements to execute if the associated statement's body did not execute.

Else Statement
Else
{
    Statements
}

Remarks

Every use of Else must belong to (be associated with) an If, Catch, For, Loop or While statement above it. An Else always belongs to the nearest applicable unclaimed statement above it unless a block is used to change that behavior. The condition for an Else statement executing depends on the associated statement:

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

if (x = 1)
    firstFunction()
else if (x = 2) ; "else if" style
    secondFunction()
else if x = 3
{
    thirdFunction()
    Sleep 1
}
else defaultFunction()  ; i.e. Any single statement can be on the same line with an Else.

Executes some code if a loop had zero iterations.

; Show File/Internet Explorer windows/tabs.
for window in ComObject("Shell.Application").Windows
    MsgBox "Window #" A_Index ": " window.LocationName
else
    MsgBox "No shell windows found."