Specifies one or more statements to execute if the associated statement's body did not execute.
Else Statement
Else
{
Statements
}
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
Common usage of an Else statement. This example is executed as follows:
if WinExist("Untitled - Notepad") { WinActivate Send "This is a test.{Enter}" } else { WinActivate "Some Other Window" MouseClick "Left", 100, 200 }