Categories: VBScript 教程

VBScript:运算符

什么是运算符?

让我们接受一个表达式4 + 5等于9。在这里,4和5称为操作数,而+称为运算符。VBScript语言支持以下类型的运算符:

  • 算术运算符
  • 比较运算符
  • 逻辑(或关系)运算符
  • 串联运算符

算术运算符

VBScript支持以下算术运算符。

假设变量A持有5,变量B持有10,则:

操作描述
+加两个操作数A + B会给15
从第一个减去第二个操作数A-B会给-5
*将两个操作数相乘A * B会给50
/用分子除以分子B / A会给2
模运算符和整数除后的余数B MOD A将给0
^求幂运算符B ^ A会给100000

尝试以下示例以了解VBScript中可用的所有算术运算符:

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

         c = a+b
         Document.write ("Addition Result is " &c)
         Document.write ("<br></br>")    'Inserting a Line Break for readability
         
         c = a-b
         Document.write ("Subtraction Result is " &c)
         Document.write ("<br></br>")   'Inserting a Line Break for readability
         
         c = a*b
         Document.write ("Multiplication Result is " &c)
         Document.write ("<br></br>")
         
         c = b/a
         Document.write ("Division Result is " &c)
         Document.write ("<br></br>")
         
         c = b MOD a
         Document.write ("Modulus Result is " &c)
         Document.write ("<br></br>")
         
         c = b^a
         Document.write ("Exponentiation Result is " &c)
         Document.write ("<br></br>")
      </script>
   </body>
</html>

当您将其另存为.html执行时,上述脚本将产生以下结果:

Addition Result is 15

Subtraction Result is -5

Multiplication Result is 50

Division Result is 2

Modulus Result is 0

Exponentiation Result is 100000

比较运算符

VBScript语言支持以下比较运算符:

操作描述
=检查两个操作数的值是否相等,如果是,则条件为真。(A == B)为False。
<>检查两个操作数的值是否相等,如果值不相等,则条件为真。(A <> B)为真。
>检查左操作数的值是否大于右操作数的值,如果是,则条件为真。(A> B)为假。
<检查左操作数的值是否小于右操作数的值,如果是,则条件为真。(A <B)为真。
> =检查左操作数的值是否大于或等于右操作数的值,如果是,则条件为真。(A> = B)为False。
<=检查左操作数的值是否小于或等于右操作数的值,如果是,则条件为true。(A <= B)为真。

假设变量A持有10,变量B持有20,则会是什么结果呢?看下实例说明:

请尝试以下示例,以了解VBScript中可用的所有比较运算符:

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

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

      If a<>b Then
         Document.write ("Operator Line 2 : True")
         Document.write ("<br></br>")
      Else
         Document.write ("Operator Line 2 : False")
         Document.write ("<br></br>")
      End If

      If a>b Then
         Document.write ("Operator Line 3 : True")
         Document.write ("<br></br>")
      Else
         Document.write ("Operator Line 3 : False")
         Document.write ("<br></br>")
      End If

      If a<b Then
         Document.write ("Operator Line 4 : True")
         Document.write ("<br></br>")
      Else
         Document.write ("Operator Line 4 : False")
         Document.write ("<br></br>")
      End If

      If a>=b Then
         Document.write ("Operator Line 5 : True")
         Document.write ("<br></br>")
      Else
         Document.write ("Operator Line 5 : False")
         Document.write ("<br></br>")
      End If

      If a<=b Then
         Document.write ("Operator Line 6 : True")
         Document.write ("<br></br>")
      Else
         Document.write ("Operator Line 6 : False")
         Document.write ("<br></br>")
      End If
 
      </script>
</body>
</html>

当将其另存为.html并执行时,上述脚本将产生以下结果:

Operator Line 1 : False

Operator Line 2 : True

Operator Line 3 : False

Operator Line 4 : True

Operator Line 5 : False

Operator Line 6 : True

逻辑运算符

VBScript语言支持以下逻辑运算符:

操作描述
AND称为逻辑AND运算符。如果两个条件都为True,则Expression为True。a <> 0 AND b <> 0为False。
OR称为逻辑或运算符。如果两个条件中的任何一个为True,则条件变为True。a <> 0或b <> 0为true。
NOT称为逻辑非运算符。它反转其操作数的逻辑状态。如果条件为True,则逻辑非运算符会将其设置为False。NOT(a <> 0或b <> 0)为假。
XOR称为逻辑排除。它是NOT和OR运算符的组合。如果只有一个表达式的计算结果为True,则结果为True。(a <> 0 XOR b <> 0)为真。

请尝试以下示例,以了解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并执行时,上述脚本将产生以下结果:

AND Operator Result is : False

OR Operator Result is : True

NOT Operator Result is : False

XOR Operator Result is : True

串联运算符

VBScript支持以下串联运算符-

假设变量A持有5,变量B持有10,则:

操作描述
+由于变量值为数字,所以将两个值相加A + B会给15
&连接两个值A和B会给510

请尝试以下示例,以了解VBScript中可用的串联运算符:

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

         c = a+b 
         Document.write ("Concatenated value:1 is " &c) 'Numeric addition 
         Document.write ("<br></br>")  'Inserting a Line Break for readability
        
         c = a&b 
         Document.write ("Concatenated value:2 is " &c) 'Concatenate two numbers 
         Document.write ("<br></br>")  'Inserting a Line Break for readability
      </script>
   </body>
</html>

当您将其另存为.html并在Internet Explorer中执行时,上述脚本将产生以下结果-

Concatenated value:1 is 15

Concatenated value:2 is 510

串联也可用于串联两个字符串。假设变量A =“ Microsoft”和变量B =“ VBScript”,然后-

操作员描述
+连接两个值A + B将提供MicrosoftVBScript
&连接两个值A和B将给MicrosoftVBScript

请尝试以下示例,以了解VBScript中可用的串联运算符:

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

         c = a+b 
         Document.write ("Concatenated value:1 is " &c) 'Numeric addition 
         Document.write ("<br></br>")  'Inserting a Line Break for readability
        
         c = a&b 
         Document.write ("Concatenated value:2 is " &c) 'Concatenate two numbers 
         Document.write ("<br></br>")  'Inserting a Line Break for readability
      </script>
   </body>
</html>

当您将其另存为.html并在Internet Explorer中执行时,上述脚本将产生以下结果:

Concatenated value:1 is MicrosoftVBScript

Concatenated value:2 is MicrosoftVBScript
terry

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

vue:页面注入js修改input值

一般会直接这样写: let z…

8 小时 ago

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago