分组条件在VBScript中是否正常工作?

时间:2015-11-16 23:19:39

标签: windows if-statement vbscript conditional boolean-logic

我在VBScript中编写了一些相当复杂的代码。我一直都知道VBScript只有按位,而不是逻辑AND和OR运算符,所以懒惰逻辑不起作用。 This MS Blog article explains it

我想知道的是(文章没有说明),如果你可以使用分组条件语句而不必求助于嵌套的IF语句。

像这样:

If A = True OR (B = True AND C = True) Then    '<-- will this statement evaluate correctly?
    ....
End If

而不是必须这样做:

If A = True Then
    If B = True AND C = True Then
        ...
    End If
End If

这会起作用吗?

此声明的其他形式是否也有效(即If A OR B (NOT C)等)?

1 个答案:

答案 0 :(得分:2)

条件

If A = True Or (B = True And C = True) Then
  ...
End If

将按照您的预期运作。没有比较操作的情况也是如此:

If A Or (B And C) Then
  ...
End If

BTW,请注意上述条件与下面的嵌套条件具有相同的含义。

If A = True Then
  If B = True And C = True Then
    ...
  End If
End If

此代码段中的语句块仅在两个条件为真时才会执行,而不是只有其中一个条件为真。相当于A Or (B And C)的(相当笨拙)可能看起来像这样:

If A = True Then
  ...
ElseIf B = True And C = True Then
  ...
End If

两个语句块包含相同的指令。