</head> <body> <h1>If</h1> <p><a href="../Variables.htm#Expressions">式</a>がtrueと評価された場合に実行する1つ以上の<a href="../Concepts.htm#statement">ステートメント</a>を指定します。</p> <pre class="Syntax"><span class="func">If</span> <i>Expression</i> { <i>Statements</i> }</pre> <h2 id="Remarks">備考</h2> <p><em>If</em>文の式がtrueと評価された場合(空文字列や数値0以外の結果)、その下の行や<a href="Block.htm">ブロック</a>が実行されます。それ以外の場合、対応する<a href="Else.htm">Else</a>文があれば、実行はその下の行またはブロックにジャンプします。</p> <p>複数の行を所有する<em>場合</em>は、それらの行を中括弧で囲む必要があります(<a href="Block.htm">ブロック</a>を作成するため)。ただし、1つの<em>If</em>に1行しか属さない場合は、中括弧は省略可能です。本ページ下部の例をご覧ください。</p> <p><code>if(expression)</code>のように、式が開括弧で始まる場合、<code>if</code>の後のスペースは任意です。</p> <p id="otb"><a href="Block.htm#otb">OTB(One True Brace)スタイル</a>はオプションで使用することができます。事例:</p> <pre>if (x &lt; y) { <em>; ...</em> } if WinExist("Untitled - Notepad") { WinActivate } if IsDone { <em>; ...</em> } else { <em>; ...</em> }</pre> <p><em>If</em>文とは異なり、<a href="Else.htm">Else</a>文はその右側にあるどのタイプの文もサポートします。</p> <h2 id="Related">関連</h2> <p><a href="../Variables.htm#Expressions">式</a>、<a href="../Variables.htm#ternary">三項演算子(a?b:c)</a>、<a href="Block.htm">ブロック</a>、<a href="Else.htm">Else</a>、<a href="While.htm">Whileループ</a></p> <h2 id="Examples">例</h2> <div class="ex" id="ExOne"> <p><a class="ex_number" href="#ExOne"></a> <var>A_Index</var>が 100 より大きい場合、リターンします。</p> <pre>if (A_Index &gt; 100) return</pre> </div> <div class="ex" id="ExMultiple"> <p><a class="ex_number" href="#ExMultiple"></a> <code>A_TickCount - StartTime</code>の結果が<code>2*MaxTime + 100</code>の結果より大きい場合、"Too much time has passed."と表示し、スクリプトを終了します。</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> この例は、次のように実行されます:</p> <ol> <li><var>Color</var>が「Blue」または「White」という単語である場合: <ol> <li>Show "The color is one of the allowed values.".</li> <li>スクリプトを終了させます。</li> </ol> </li> <li>それ以外の場合、<var>Color</var>が"Silver"である場合: <ol> <li>Show "Silver is not an allowed color.".</li> <li>それ以上のチェックを停止します。</li> </ol> </li> <li>それ以外: <ol> <li>Show "This color is not recognized.".</li> <li>スクリプトを終了させます。</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> 1つの<a href="../Variables.htm#comma">複数文</a>の行は、中括弧で囲む必要はありません。</p> <pre>MyVar := 3 if (MyVar &gt; 2) MyVar++, MyVar := MyVar - 4, MyVar .= " test" MsgBox MyVar <em>; "0 test"と表示されます。</em> </pre> </div> <div class="ex" id="ExIfBetween"> <p><a class="ex_number" href="#ExIfBetween"></a> Similar to AutoHotkey v1's <a href="https://www.autohotkey.com/docs/v1/lib/IfBetween.htm">If Var [not] between Lower and Upper</a>, the following examples check whether a <a href="../Variables.htm">variable's</a> contents are numerically or alphabetically between two values (inclusive).</p> <p>Checks whether <var>var</var> is in the range 1 to 5:</p> <pre>if (var &gt;= 1 and var &lt;= 5) MsgBox var " is in the range 1 to 5, inclusive."</pre> <p>Checks whether <var>var</var> is in the range 0.0 to 1.0:</p> <pre>if not (var &gt;= 0.0 and var &lt;= 1.0) MsgBox var " is not in the range 0.0 to 1.0, inclusive."</pre> <p>Checks whether <var>var</var> is between <var>VarLow</var> and <var>VarHigh</var> (inclusive):</p> <pre>if (var &gt;= VarLow and var &lt;= VarHigh) MsgBox var " is between " VarLow " and " VarHigh "."</pre> <p>Checks whether <var>var</var> is alphabetically between the words blue and red (inclusive):</p> <pre>if (StrCompare(var, "blue") &gt;= 0) and (StrCompare(var, "red") &lt;= 0) MsgBox var " is alphabetically between the words blue and red."</pre> <p>Allows the user to enter a number and checks whether it is in the range 1 to 10:</p> <pre>LowerLimit := 1 UpperLimit := 10 IB := InputBox("Enter a number between " LowerLimit " and " UpperLimit) if not (IB.Value &gt;= LowerLimit and IB.Value &lt;= UpperLimit) MsgBox "Your input is not within the valid range."</pre> </div> <div class="ex" id="ExIfInContains"> <p><a class="ex_number" href="#ExIfInContains"></a> Similar to AutoHotkey v1's <a href="https://www.autohotkey.com/docs/v1/lib/IfIn.htm">If Var [not] in/contains MatchList</a>, the following examples check whether a <a href="../Variables.htm">variable's</a> contents match one of the items in a list.</p> <p>Checks whether <var>var</var> is the file extension exe, bat or com:</p> <pre>if (var ~= "i)\A(exe|bat|com)\z") MsgBox "The file extension is an executable type."</pre> <p>Checks whether <var>var</var> is the prime number 1, 2, 3, 5, 7 or 11:</p> <pre>if (var ~= "\A(1|2|3|5|7|11)\z") MsgBox var " is a small prime number."</pre> <p>Checks whether <var>var</var> contains the digit 1 or 3:</p> <pre>if (var ~= "1|3") MsgBox "Var contains the digit 1 or 3 (Var could be 1, 3, 10, 21, 23, etc.)"</pre> <p>Checks whether <var>var</var> is one of the items in <var>MyItemList</var>:</p> <pre><em>; Uncomment the following line if MyItemList contains RegEx chars except | ; MyItemList := RegExReplace(MyItemList, "[\Q\.*?+[{()^$\E]", "\$0")</em> if (var ~= "i)\A(" MyItemList ")\z") MsgBox var " is in the list."</pre> <p>Allows the user to enter a string and checks whether it is the word yes or no:</p> <pre>IB := InputBox("Enter YES or NO") if not (IB.Value ~= "i)\A(yes|no)\z") MsgBox "Your input is not valid."</pre> <p>Checks whether <var>active_title</var> contains "Address List.txt" or "Customer List.txt" and checks whether it contains "metapad" or "Notepad":</p> <pre>active_title := WinGetTitle("A") if (active_title ~= "i)Address List\.txt|Customer List\.txt") MsgBox "One of the desired windows is active." if not (active_title ~= "i)metapad|Notepad") MsgBox "But the file is not open in either Metapad or Notepad."</pre> </div> </body> </html>