</head> <body> <h1>If (Expression)</h1> <p>Specifies one or more <a href="../Concepts.htm#statement">statements</a> to execute if an <a href="../Variables.htm#Expressions">expression</a> evaluates to true.</p> <pre class="Syntax"><span class="func">If</span> (<i>Expression</i>) { <i>Statements</i> }</pre> <h2 id="Remarks">Remarks</h2> <p>An <em>If</em> statement that contains an expression is usually differentiated from a <a href="IfEqual.htm">traditional If statement</a> such as <code>if FoundColor != Blue</code> by enclosing the expression in parentheses, as in <code>if (FoundColor != "Blue")</code>. However, this is not strictly required, as any <em>If</em> statement which does not match any of the <a href="../Language.htm#legacy-if">legacy if patterns</a> is assumed to contain an expression. In particular, the following are also common ways of writing an <em>If (expression)</em>:</p> <ul> <li>Starting with an open parenthesis but not fully enclosing the expression: <code>if (x &gt; 0) and (y &gt; 0)</code></li> <li>Starting with a <a href="../Functions.htm">function call</a>: <code>if InStr(a, b)</code></li> <li>Starting with an <a href="../Variables.htm#Operators">operator</a> such as <code>not</code> or <code>!</code>: <code>if !MyVar</code></li> </ul> <p><strong>Known limitation:</strong> For historical reasons, <em>If (expression)</em> actually accepts a <a href="../Language.htm#numeric-parameters">numeric parameter</a> rather than a pure expression. For example, <code>if %MyVar%</code> is equivalent to <code>if MyVar</code>. This can be avoided by always enclosing the expression in parentheses.</p> <p>If the <em>If</em> statement's expression evaluates to true (which is any result other than an empty string or the number 0), the line or <a href="Block.htm">block</a> underneath it is executed. Otherwise, if there is a corresponding <a href="Else.htm">Else</a> statement, execution jumps to the line or block underneath it.</p> <p>If an <em>If</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>If</em>, the braces are optional. See the examples at the bottom of this page.</p> <p>The space after <code>if</code> is optional if the expression starts with an open-parenthesis, as in <code>if(expression)</code>.</p> <p id="otb">The One True Brace (OTB) style may optionally be used with <em>If</em> statements that are expressions (but not <a href="IfEqual.htm">traditional If statements</a>). Por exemplo:</p> <pre>if (x &lt; y) { <em>; ...</em> } if WinExist("Untitled - Notepad") { WinActivate } if IsDone { <em>; ...</em> } else { <em>; ...</em> }</pre> <p>Unlike an <em>If</em> statement, an <a href="Else.htm">Else</a> statement supports any type of statement immediately to its right.</p> <p>On a related note, the statement <code><a href="IfBetween.htm">if Var between LowerBound and UpperBound</a></code> checks whether a variable is between two values, and <code><a href="IfIn.htm">if Var in MatchList</a></code> can be used to check whether a variable's contents exist within a list of values.</p> <h2 id="Related">Tópicos relacionados</h2> <p><a href="../Variables.htm#Expressions">Expressions</a>, <a href="SetExpression.htm">Assign expression (:=)</a>, <a href="IfIn.htm">if var in/contains</a>, <a href="IfBetween.htm">if var between</a>, <a href="IfInString.htm">IfInString</a>, <a href="Block.htm">Blocks</a>, <a href="Else.htm">Else</a>, <a href="While.htm">While-loop</a></p> <h2 id="Examples">Exemplos</h2> <div class="ex" id="ExOne"> <p><a class="ex_number" href="#ExOne"></a> If <var>A_Index</var> is greater than 100, return.</p> <pre>if (A_Index &gt; 100) return</pre> </div> <div class="ex" id="ExMultiple"> <p><a class="ex_number" href="#ExMultiple"></a> If the result of <code>A_TickCount - StartTime</code> is greater than the result of <code>2*MaxTime + 100</code>, show "Too much time has passed." and terminate the script.</p> <pre>if (A_TickCount - StartTime &gt; 2*MaxTime + 100) { MsgBox Too much time has passed. ExitApp }</pre> </div> <div class="ex" id="ExComplex"> <p><a class="ex_number" href="#ExComplex"></a> This example is executed as follows:</p> <ol> <li>If <var>Color</var> is the word "Blue" or "White": <ol> <li>Show "The color is one of the allowed values.".</li> <li>Terminate the script.</li> </ol> </li> <li>Otherwise if <var>Color</var> is the word "Silver": <ol> <li>Show "Silver is not an allowed color.".</li> <li>Stop further checks.</li> </ol> </li> <li>Do contrário: <ol> <li>Show "This color is not recognized.".</li> <li>Terminate the script.</li> </ol> </li> </ol> <pre>if (Color = "Blue" or Color = "White") { MsgBox The color is one of the allowed values. ExitApp } else if (Color = "Silver") { MsgBox Silver is not an allowed color. return } else { MsgBox This color is not recognized. ExitApp }</pre> </div> <div class="ex" id="ExMultiStatement"> <p><a class="ex_number" href="#ExMultiStatement"></a> A single <a href="../Variables.htm#comma">multi-statement</a> line does not need to be enclosed in braces.</p> <pre>MyVar := 3 if (MyVar &gt; 2) MyVar++, MyVar := MyVar - 4, MyVar .= " test" MsgBox % MyVar <em>; Reports "0 test".</em> </pre> </div> </body> </html>