什么是运算符?
让我们接受一个表达式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,如若转载,请注明出处:https://www.web176.com/vbscript/1307.html
支付宝
微信