Operators in VBScript. Arithmetic Operators


© Maxim Karetnikov
Articles in this Topic    Discussions in this Topic

VBScript provides several different operators that can be used when comparing values. There are basically four different types of operators available.They are as follows:

  • Computation to perform mathematical calculations
  • Comparison to Compare two values
  • Logical for logical operations
  • Concatenation for string manipulations

Arithmetic Operators

All of you are familiar with mathematical operators from the everyday life. You are using mathematical expressions to calculate something, e.g., taxes, age, distance. It takes two things to make a mathematical expression: values (numbers) and operators. When you add two numbers using a calculator, you enter the first number, press the plus-sign (+), enter the second number, and press the Enter key. The plus-sign is an operator. It performs an operation on two numbers. Likewise, the minus (-), multiplication (*), and division signs (/) are operators.

A variable name is an expression, and the addition of two values is an expression. You can use the result of an expression just as you use a value. You can assign it to a variable using the assignment operator (=), like this:

Result = Number1 * Number2

You can pass it to the built-in VBScript function such as MsgBox, like this:

MsgBox Number1 * Number2

Finally, You can pass an expression to the procedure, like this:

  Call MyProcedure( Number1 * Number2) 

Addition

The addition operator is used to add values, whether they are stored in variables, constants, or literal numbers. In VBScript You can add variables, numbers, and constants in a variety of combinations with the addition operator. For example, you can use the addition operator to add two numbers together and assign them to a variable:

Result = 2 + 3

You can also add a number to a variable and assign it to another variable:

Result = Number1 + 15

You can modify the variable:

Result = Result + 1

You can add as many numbers, variables, and constants as you want:

Result = Number1 + 5 +Constant1 + 8 + Variable1 

Subtraction

The subtraction operator should  be also very familiar to you. This operator works the same way the addition operator does except that it subtracts one or more numbers rather than add them. The syntax is the same.

Unary Operators

It gets the name from the fact that it is a unary operator. That is, it operates on only one value as opposed to the other operators (binary) which operate on two values. It is a very simple operator that negates a number. It turns a positive number into a negative number, and vice versa. For example, if intNumber contains the number 6, -intNumber is -6. Here is the syntax for unary negation:

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo