If (表达式)
{
语句
}
如果 If 语句的表达式的计算结果为 true(即除空字符串或数字 0 以外的任何结果), 则执行其下面的行或区块. 否则, 如果存在相应的 Else 语句, 则执行将跳转到其下面的行或区块.
如果 If 拥有多行, 那么这些行必须用大括号括起来(创建一个区块). 但是, 如果只有一行属于 If, 则大括号是可选的. 请参阅此页面底部的示例.
如果表达式以小括号开头, 则 if 后面的空格是可选的, 例如 if(expression).
可以选择使用 One True Brace(OTB) 样式. 例如:
if (x < y) {
; ...
}
if WinExist("Untitled - Notepad") {
WinActivate
}
if IsDone {
; ...
} else {
; ...
}
与 If 语句不同, Else 语句支持任何类型的语句紧跟在其右边.
表达式, 三元运算符(a?b:c), 区块, Else, While-loop
如果 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
}
单个多语句行不需要用大括号括起来.
MyVar := 3
if (MyVar > 2)
MyVar++, MyVar := MyVar - 4, MyVar .= " test"
MsgBox % MyVar ; 报告 "0 test".