一对大括号组成的区域表示一个区块。区块通常与函数、Else、Loop、While 循环和 IF 命令一起使用。
{
零行或多行命令
}
使用区块可以绑定两行或多行命令. 还可以用它改变 IF 与 ELSE 从属关系, 例如在此例中, 区块强制 ELSE 从属于首个 IF 而不是第二个:
if var1 = 1
{
if var2 = abc
sleep, 1
}
else
return
尽管区块可以用在脚本中的任何位置, 不过目前它们仅在与 函数, Else, Loop 或 IF 类型的命令例如 IfEqual 或 IfWinExist 一起使用时才有意义.
如果 IF, ELSE, Loop, While 循环 或 For 循环 中仅含单行命令, 那么此命令可以不需要括在区块中. 不过, 这样做也许可以提高脚本的可读性和可维护性.
区块中可以为空 (不含任何命令), 当您想注释区块中的内容而不移除区块时这很有用.
One True Brace(OTB,K&R风格):在后面这些位置中可以使用OTB风格:表达式型if语句、else关键字、while循环、For循环、普通循环、函数定义、Try、Catch和Finally。这种风格中区块的开括号与区块的控制语句在同一行, 而不是在其下一行. 例如:
if (x < y) {
...
} else {
...
}
While x < y {
...
}
For k,v in obj {
...
}
Loop %RepeatCount% {
...
}
MyFunction(x, y) {
...
}
Try {
...
} Catch e {
...
} Finally {
....
}
同样地, 命令或其他操作可以放在大括号的后面 (但不包括 One True Brace 风格的开括号). 例如:
if x = 1
{ MsgBox This line appears to the right of an opening brace. It executes whenever the IF-statement is true.
MsgBox This is the next line.
} MsgBox This line appears to the right of a closing brace. It executes unconditionally.
函数, While 循环, Loop, Else, If, If(Expression)
if x = 1
{
MsgBox, test1
Sleep, 5
}
else
MsgBox, test2