</head> <body> <h1>Else</h1> <p>Specifies one or more <a href="../Concepts.htm#statement">statements</a> to execute if an <a href="../Language.htm#if-statement">If statement</a> evaluates to false.</p> <pre class="Syntax"><span class="func">Else</span> <i>Statement</i></pre> <pre class="Syntax"><span class="func">Else</span> { <i>Statements</i> }</pre> <h2 id="Remarks">Remarks</h2> <p>Every use of an <em>Else</em> must belong to (be associated with) an <a href="../Language.htm#if-statement">If statement</a> above it. An <em>Else</em> always belongs to the nearest unclaimed If statement above it unless a <a href="Block.htm">block</a> is used to change that behavior.</p> <p>An <em>Else</em> can be followed immediately by any other single <a href="../Concepts.htm#statement">statement</a> on the same line. This is most often used for "else if" ladders (see examples at the bottom).</p> <p>If an <em>Else</em> owns more than one line, those lines must be enclosed in braces (to create a <a href="Block.htm">block</a>). However, if only one line belongs to an <em>Else</em>, the braces are optional. For example:</p> <pre>if (count &gt; 0) <em>; No braces are required around the next line because it's only a single line.</em> MsgBox Press OK to begin the process. else <em>; Braces must be used around the section below because it consists of more than one line.</em> { WinClose Untitled - Notepad MsgBox There are no items present. }</pre> <p>The <a href="Block.htm#otb">One True Brace (OTB) style</a> may optionally be used around an <em>Else</em>. For example:</p> <pre>if IsDone { <em>; ...</em> } else if (x &lt; y) { <em>; ...</em> } else { <em>; ...</em> }</pre> <h2 id="Related">Related</h2> <p><a href="Block.htm">Blocks</a>, <a href="../Language.htm#if-statement">If Statements</a>, <a href="../Language.htm#control-flow">Control Flow Statements</a></p> <h2 id="Examples">Examples</h2> <div class="ex" id="ExBasic"> <p><a class="ex_number" href="#ExBasic"></a> Common usage of an <em>Else</em> statement. This example is executed as follows:</p> <ol> <li>If Notepad exists: <ol> <li>Activate it</li> <li>Send the string "This is a test." followed by <kbd>Enter</kbd>.</li> </ol> </li> <li>Otherwise (that is, if Notepad does not exist): <ol> <li>Activate another window</li> <li>Left-click at the coordinates 100, 200</li> </ol> </li> </ol> <pre>if WinExist("Untitled - Notepad") { WinActivate Send This is a test.{Enter} } else { WinActivate, Some Other Window MouseClick, Left, 100, 200 }</pre> </div> <div class="ex" id="ExOtherUsages"> <p><a class="ex_number" href="#ExOtherUsages"></a> Demonstrates different styles of how the <em>Else</em> statement can be used too. Note that IfEqual is deprecated and should generally be avoided.</p> <pre>if (x = 1) Gosub, a1 else if (x = 2) <em>; "else if" style</em> Gosub, a2 else IfEqual, x, 3 <em>; alternate style</em> { Gosub, a3 Sleep, 1 } else Gosub, a4 <em>; i.e. Any single statement can be on the same line with an Else.</em> <em>; Also OK:</em> IfEqual, y, 1, Gosub, b1 else { Sleep, 1 Gosub, b2 }</pre> </div> </body> </html>