VBScript operator

From StealthBot Wiki Backup
(Redirected from Order of operations)
Jump to: navigation, search

VBScript operators allow you to compare values, do math with values, or put values together. They perform operations on values. Many VBScript operators perform different things based on context.

Using operators

In VBScript, operators perform operations to two operands (one operand in the case of the Not operator), many times the resulting action can depend on the context or content of operands.

Operators are evaluated in the "order of operations", similar to what you may have learned in math class.

Order of operations

The order of operation is the order that operations are done. Operators of the same type act from left to right, but of different type act depending on an order that was meant to make the most sense. Use parentheses (( and )) to force certain operations to occur before others.

Operators with a higher precedence number are evaluated first.[1]

Precedence Operator Description
14 (...) Operations in parentheses
13 ., Call Object member access, and other function, property, and variable evaluation
12 ^ Exponent
11 - Unary negation
10 *, /, \, Mod Multiplication, division (decimal), division (integer), modulus
9 +, - Addition, subtraction
8 &, + String concatenation
7 =, <>, >, <, >=, <=, Is Equal to, greater than, less than, greater than or equal to, less than or equal to, not equal to, object references equal to
6 Not Logical or bitwise negation
5 And Logical or bitwise conjunction
4 Or Logical or bitwise disjunction
3 Xor Logical or bitwise exclusion
2 Eqv Logical or bitwise equivalence
1 Imp Logical or bitwise implication
0 =, Set ... = Assignment operator

Parentheses

Operations in parentheses are the highest precedence so that you can use them to evaluate specific parts of an expression before other parts, similar to their meaning in mathematical expressions.

Argument lists in functions are technically in parentheses so the set of arguments being passed to a function or subroutine are evaluated before the function processes them.

Assignment is always the last evaluated operator, and trying to use parentheses to change this will result in errors.

Examples:

Value = CreateObject("Scripting.Dictionary").Count

Here, the CreateObject() function will return a new dictionary, then the result will be accessed for the Count property which will then be assigned to Value. This will not try to assign the object to Value.

If 1 + 2 * 3 ^ 4 <> ((1 + 2) * 3) ^ 4 Then

This if block will return True, illustrating that the mathematical operations will be evaluated first, which goes in order: exponent, multiplication, addition; then the inequality will be evaluated.

Assignment operator: =

The Assignment operator, =, allows you to assign the variable, value, or result of another operation or function on the right to the variable on the left.

Example: Store a literal value into Var1

Var1 = "a literal string value"

Example: Store the value of Var2 in Var1

Var1 = Var2

Example: Store the result of a function into Var1 called SomeFunction with the variable SomeArgument passed to it.

Var1 = SomeFunction(SomeArgument)

Concatenation operator: &

The & operator takes two values and returns the result of them as a String.

Var1 = "H" & "ello"

Var1 will contain the string "Hello".

Var1 = "The number is " & 1

Var1 will contain the string "The number is 1".

Var1 = 1 & 1

Var1 will contain the string "11" even though neither operands are a string. In all cases, a variable or function call could be substituted for the literals in these examples.

It is also valid to use the + operator between two string values to concatenate them, but you have to make sure one operator is string or it will attempt to add the values.

Comparison operators

Template:Main Comparison operators compare two operands and return a Boolean indicating success (True) or failure (False).

Logical operators

Template:Main Logical operators are a type of comparison operator that takes two Booleans and returns another Boolean, such as the results of other comparison operators.

Arithmetic operators

Template:Main Arithmetic operators take two numeric operands and return a Double result.

Bitwise operators

Template:Main Bitwise operators are a type of arithmetic operator that takes two Integers and returns another Integer. In VBScript, the same operators that are logical operators are also bitwise operators. "Bitwise" meaning that they operate on each "bit" or part of the number in binary, basically doing the same operation as their logical counterpart but to each of the bits.

See also