式がtrueと評価された場合に実行する1つ以上のステートメントを指定します。
If Expression
{
Statements
}
If文の式がtrueと評価された場合(空文字列や数値0以外の結果)、その下の行やブロックが実行されます。それ以外の場合、対応するElse文があれば、実行はその下の行またはブロックにジャンプします。
複数の行を所有する場合は、それらの行を中括弧で囲む必要があります(ブロックを作成するため)。ただし、1つのIfに1行しか属さない場合は、中括弧は省略可能です。本ページ下部の例をご覧ください。
if(expression)
のように、式が開括弧で始まる場合、if
の後のスペースは任意です。
OTB(One True Brace)スタイルはオプションで使用することができます。事例:
if (x < y) { ; ... } if WinExist("Untitled - Notepad") { WinActivate } if IsDone { ; ... } else { ; ... }
If文とは異なり、Else文はその右側にあるどのタイプの文もサポートします。
式、三項演算子(a?b:c)、ブロック、Else、Whileループ
A_TickCount - StartTime
の結果が2*MaxTime + 100
の結果より大きい場合、"Too much time has passed."と表示し、スクリプトを終了します。
if (A_TickCount - StartTime > 2*MaxTime + 100) { MsgBox "Too much time has passed." ExitApp }
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 }
1つの複数文の行は、中括弧で囲む必要はありません。
MyVar := 3 if (MyVar > 2) MyVar++, MyVar := MyVar - 4, MyVar .= " test" MsgBox MyVar ; "0 test"と表示されます。
Similar to AutoHotkey v1's If Var [not] between Lower and Upper, the following examples check whether a variable's contents are numerically or alphabetically between two values (inclusive).
Checks whether var is in the range 1 to 5:
if (var >= 1 and var <= 5) MsgBox var " is in the range 1 to 5, inclusive."
Checks whether var is in the range 0.0 to 1.0:
if not (var >= 0.0 and var <= 1.0) MsgBox var " is not in the range 0.0 to 1.0, inclusive."
Checks whether var is between VarLow and VarHigh (inclusive):
if (var >= VarLow and var <= VarHigh) MsgBox var " is between " VarLow " and " VarHigh "."
Checks whether var is alphabetically between the words blue and red (inclusive):
if (StrCompare(var, "blue") >= 0) and (StrCompare(var, "red") <= 0) MsgBox var " is alphabetically between the words blue and red."
Allows the user to enter a number and checks whether it is in the range 1 to 10:
LowerLimit := 1 UpperLimit := 10 IB := InputBox("Enter a number between " LowerLimit " and " UpperLimit) if not (IB.Value >= LowerLimit and IB.Value <= UpperLimit) MsgBox "Your input is not within the valid range."
Similar to AutoHotkey v1's If Var [not] in/contains MatchList, the following examples check whether a variable's contents match one of the items in a list.
Checks whether var is the file extension exe, bat or com:
if (var ~= "i)\A(exe|bat|com)\z") MsgBox "The file extension is an executable type."
Checks whether var is the prime number 1, 2, 3, 5, 7 or 11:
if (var ~= "\A(1|2|3|5|7|11)\z") MsgBox var " is a small prime number."
Checks whether var contains the digit 1 or 3:
if (var ~= "1|3") MsgBox "Var contains the digit 1 or 3 (Var could be 1, 3, 10, 21, 23, etc.)"
Checks whether var is one of the items in MyItemList:
; Uncomment the following line if MyItemList contains RegEx chars except | ; MyItemList := RegExReplace(MyItemList, "[\Q\.*?+[{()^$\E]", "\$0") if (var ~= "i)\A(" MyItemList ")\z") MsgBox var " is in the list."
Allows the user to enter a string and checks whether it is the word yes or no:
IB := InputBox("Enter YES or NO") if not (IB.Value ~= "i)\A(yes|no)\z") MsgBox "Your input is not valid."
Checks whether active_title contains "Address List.txt" or "Customer List.txt" and checks whether it contains "metapad" or "Notepad":
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."