如果相关联语句的主体没有执行, 指定一个或多个语句去执行.
Else 语句
Else
{
语句
}
Else 的每次使用都必须附属于(与之关联的) 其上方的 If, Catch, For, Loop 或 While 语句. Else 始终附属于其上方最近的且无主的语句, 除非使用区块来改变该行为. Else 语句执行的条件取决于关联语句:
Else 可以在同一行上紧跟着任何其他的单个语句. 这通常用于 "else if" 梯形结构(请参阅底部的例子).
如果 Else 拥有多行, 那么这些行必须用大括号括起来(创建一个区块). 但是, 如果只有一行属于 Else, 则大括号是可选的. 例如:
if (count > 0) ; 下一行可以不需要使用大括号括住, 因为它只有单独一行.
MsgBox "Press OK to begin the process."
else ; 下面这部分必须使用大括号括起来, 因为它含有多行.
{
WinClose "Untitled - Notepad"
MsgBox "There are no items present."
}
One True Brace(OTB) 风格可以用于 Else 前后. 例如:
if IsDone {
; ...
} else if (x < y) {
; ...
} else {
; ...
}
if WinExist("Untitled - Notepad")
{
WinActivate
Send "This is a test.{Enter}"
}
else
{
WinActivate "Some Other Window"
MouseClick "Left", 100, 200
}
if (x = 1)
firstFunction()
else if (x = 2) ; "else if" 类型
secondFunction()
else if x = 3
{
thirdFunction()
Sleep 1
}
else defaultFunction() ; 即任何单个语句都可以和 Else 放在同一行.