VBScript 邏輯運算子



VBScript 支援以下邏輯運算子:

假設變數 A 為 10,變數 B 為 0,則:

運算子 描述 示例
AND 稱為邏輯 AND 運算子。如果兩個條件都為 True,則表示式為 True。 a<>0 AND b<>0 為 False。
OR 稱為邏輯 OR 運算子。如果兩個條件中任何一個為 True,則條件為 True。 a<>0 OR b<>0 為 True。
NOT 稱為邏輯 NOT 運算子。它反轉其運算元的邏輯狀態。如果一個條件為 True,則邏輯 NOT 運算子將使其為 False。 NOT(a<>0 OR b<>0) 為 False。
XOR 稱為邏輯異或。它是 NOT 和 OR 運算子的組合。如果只有一個表示式計算結果為 True,則結果為 True。 (a<>0 XOR b<>0) 為 True。

示例

嘗試以下示例以瞭解 VBScript 中可用的所有邏輯運算子:

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim a : a = 10
         Dim b : b = 0 
         Dim c

         If a<>0 AND b<>0 Then                    
            Document.write ("AND Operator Result is : True")
            Document.write ("<br></br>")  'Inserting a Line Break for readability
         Else
            Document.write ("AND Operator Result is : False")
            Document.write ("<br></br>")  'Inserting a Line Break for readability
         End If

         If a<>0 OR b<>0 Then
            Document.write ("OR Operator Result is : True")
            Document.write ("<br></br>")
         Else
            Document.write ("OR Operator Result is : False")
            Document.write ("<br></br>") 
         End If

         If NOT(a<>0 OR b<>0) Then
            Document.write ("NOT Operator Result is : True")
            Document.write ("<br></br>") 
         Else
            Document.write ("NOT Operator Result is : False")
            Document.write ("<br></br>") 
         End If

         If (a<>0 XOR b<>0) Then
            Document.write ("XOR Operator Result is : True")
            Document.write ("<br></br>") 
         Else
            Document.write ("XOR Operator Result is : False")
            Document.write ("<br></br>") 
         End If
      </script>
   </body>
</html>

將以上指令碼儲存為 .html 檔案並在 Internet Explorer 中執行它,則以上指令碼將產生以下結果:

AND Operator Result is : False

OR Operator Result is : True

NOT Operator Result is : False

XOR Operator Result is : True
vbscript_operators.htm
廣告