VB 条件
If / ElseIf / Select Case、逻辑运算符与真假判断。
If
Dim score
score = 85
If score >= 90 Then
WScript.Echo "优秀"
ElseIf score >= 60 Then
WScript.Echo "及格"
Else
WScript.Echo "不及格"
End If及格ElseIf is one word, not Else If. Two words is a syntax error in VBScript — a common slip for people coming from VB.NET, which accepts both.
单行 If
Dim x
x = 5
If x > 0 Then WScript.Echo "正数"
If x > 0 Then WScript.Echo "正数" Else WScript.Echo "非正数"正数
正数The single-line form takes no End If and only one statement. Beyond that use the block form; joining statements with : in a single-line If behaves unpredictably.
Select Case
Dim code
code = "B"
Select Case code
Case "A"
WScript.Echo "优秀"
Case "B", "C"
WScript.Echo "良好"
Case "D"
WScript.Echo "及格"
Case Else
WScript.Echo "不及格"
End Select良好Case "B", "C" matches several values in one branch. Select Case reads better than a chain of ElseIf and evaluates the expression once, whereas each ElseIf re-evaluates it.
区间与比较
Dim n
n = 75
Select Case True ' 常用技巧:用 True 做区间判断
Case n >= 90
WScript.Echo "A"
Case n >= 60
WScript.Echo "B"
Case Else
WScript.Echo "C"
End SelectBVBScript's Select Case has no Case 60 To 89 range syntax (that is VB.NET). Select Case True with boolean expressions achieves the same and appears often in legacy code.
逻辑运算符
Dim a, b
a = True : b = False
WScript.Echo a And b ' False
WScript.Echo a Or b ' True
WScript.Echo Not a ' False
WScript.Echo a Xor b ' True
WScript.Echo (1 = 1) And (2 > 1) ' TrueFalse
True
False
True
TrueAnd / Or do not short-circuit: both sides are evaluated. So If obj Is Nothing Or obj.Name = "" Then still touches obj.Name when obj is Nothing and fails. Nest the Ifs instead.
真假判断
If 0 Then WScript.Echo "0 为真" Else WScript.Echo "0 为假"
If 1 Then WScript.Echo "1 为真"
If -1 Then WScript.Echo "-1 为真"
If "" Then WScript.Echo "空串为真" Else WScript.Echo "空串为假"0 为假
1 为真
-1 为真
空串为假Numeric 0 is false and any non-zero value true (including -1). Empty strings are false. Null makes the whole condition Null, so If Null Then takes the Else branch — a frequent source of subtle bugs.
Request may be the string "0", which is true in an If (non-empty string). Convert with CInt first, or If Request("flag") Then enters the true branch for "0".If
Dim score
score = 85
If score >= 90 Then
WScript.Echo "优秀"
ElseIf score >= 60 Then
WScript.Echo "及格"
Else
WScript.Echo "不及格"
End If及格ElseIf 是一个词,不是 Else If。写成两个词在 VBScript 里是语法错误——这是从 VB.NET 转过来的人常犯的错,那边两种写法都有。
单行 If
Dim x
x = 5
If x > 0 Then WScript.Echo "正数"
If x > 0 Then WScript.Echo "正数" Else WScript.Echo "非正数"正数
正数单行写法不能有 End If,且只能放一条语句。超过一条就必须换多行写法——单行 If 加 : 拼多条语句时行为容易出错,不建议用。
Select Case
Dim code
code = "B"
Select Case code
Case "A"
WScript.Echo "优秀"
Case "B", "C"
WScript.Echo "良好"
Case "D"
WScript.Echo "及格"
Case Else
WScript.Echo "不及格"
End Select良好Case "B", "C" 一个分支匹配多个值。Select Case 比一长串 ElseIf 清楚,而且只对表达式求值一次——ElseIf 每个分支都要重新求值。
区间与比较
Dim n
n = 75
Select Case True ' 常用技巧:用 True 做区间判断
Case n >= 90
WScript.Echo "A"
Case n >= 60
WScript.Echo "B"
Case Else
WScript.Echo "C"
End SelectBVBScript 的 Select Case 不支持 Case 60 To 89 这种区间写法(那是 VB.NET 的)。用 Select Case True 配布尔表达式能达到同样效果,也是老代码里的常见写法。
逻辑运算符
Dim a, b
a = True : b = False
WScript.Echo a And b ' False
WScript.Echo a Or b ' True
WScript.Echo Not a ' False
WScript.Echo a Xor b ' True
WScript.Echo (1 = 1) And (2 > 1) ' TrueFalse
True
False
True
TrueAnd / Or 不短路——两边都会求值。所以 If obj Is Nothing Or obj.Name = "" Then 会在 obj 为 Nothing 时仍然访问 obj.Name,直接报错。必须拆成嵌套 If。
真假判断
If 0 Then WScript.Echo "0 为真" Else WScript.Echo "0 为假"
If 1 Then WScript.Echo "1 为真"
If -1 Then WScript.Echo "-1 为真"
If "" Then WScript.Echo "空串为真" Else WScript.Echo "空串为假"0 为假
1 为真
-1 为真
空串为假数值 0 为假,非 0 为真(-1 也是真)。空串为假,非空串为真。Null 参与判断时结果是 Null,If Null Then 走的是 Else 分支——这是很多隐蔽 bug 的来源。
Request 拿到的值可能是字符串 "0",它在 If 里是真(非空串)。判断数值前先 CInt 转换,否则 If Request("flag") Then 对 "0" 也会进入真分支。