2: Operators
Manipulating VBScript Variables with Operators
As you have already seen the VBScript = assignment operator is used to assign a value to a variable. When you are working with variables that contain numeric values, you can use any of the VBScript arithmetic operators, shown in Table, to change the value assigned to variables.
| Arithmetic Operators | |
| Operator | Description |
| ^ | Exponentiation |
| - | Negation |
| * | Multiplication |
| / | Division |
| \ | Integer division |
| Mod | Modulus |
| + | Addition |
| - | Subtraction |
| & | Concatenation |
The following VBScript statements show how you can apply some of the operators.
Option Explicit Dim myExp myExp = 37 WScript.Echo " myExp= " & myAge myExp = myExp + 3 WScript.Echo " myExp = " & myAge myExp = myExp - 10 WScript.Echo " myExp = " & myExp myExp = myExp * 2 WScript.Echo " myExp = " & myExp
If you were to save this script as part of a WSH VBScript and run it from the CScript.exe execution host then you’d see the output shown here.
myExp = 37 myExp = 40 myExp = 30 myExp = 60
VBScript performs arithmetic operations using a strict set of rules known as the order of operator precedence.
Comparison Operators
VBScript provides a collection of operators that you can use to compare the values of expressions within your VBScripts. These operators are listed inTable.
| Comparison Operators | |
| Operator | Description |
| = | Equal |
| <> | Not equal |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
Testing the values of variables in your scripts is something that you’ll find yourself doing again and again. When combined with conditional execution logic, which I’ll cover in just a bit, you’ll be able to write scripts that can test for certain conditions and then switch between multiple execution paths based on the results of those tests.Unlike arithmetic operators, there is no order of precedence to the execution of comparison operators. They are executed in the order in which they appear in a statement (e.g., they are evaluated starting from left to right).For example, the following statement tests to see if values of two variables are equal.
Option Explicit Dim myExp, yourExpIf myExp = yourExp Then WScript.Echo "Congrates!!!" End If
Similarly, you can test for greater than or less than values.
If myExp > yourExp Then WScript.Echo "May I Check it?" End If If myExp < yourExp Then WScript.Echo "Would you like to Check it?" End If
[...] Next Step – Operators [...]
Pingback by 1: Declaring Variables « KR Testing Solutions | February 5, 2008 |