Working with operators in VBScript

Discussion in 'JavaScript and AJAX' started by Sanskruti, Apr 18, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    An operator is a symbol applied to data values that causes the computer to carry out a specific operation on that data . In VBScript, operators are used to perform an operation. For example, an operator could be used to assign a value to a variable. An operator could also be used to compare two values. Below is a listing of VBScript operators and a brief description of them.

    Arithmetic Operators



    The first major class of operators is arithmetic operators. Arithmetic operators enable you to perform simple arithmetic on one or more variables. Most of these operators will be familiar to you because you have been exposed to them in everyday life. Arithmetic operators are a class of operators specifically intended to perform arithmetic on the corresponding data.

    Addition (+)



    The first arithmetic operator is the addition operator. The addition operator is used to add values, whether they are stored in variables, constants, or literal numbers. You also use the + operator to concatenate strings. 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:

    Code:
    Product = 2 + 3 
    You can also add a number to a variable and assign it to another variable:
    Code:
    Result = Quantity + 15 

    Subtraction (-)



    The subtraction operator should also be 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. Otherwise, the syntax is the same. You can subtract two numbers and assign the result to a variable:
    Code:
    Result = 100 - 37 
    You can also use variables:
    Code:
    Result = 100 - Count

    Multiplication (*)



    Addition and subtraction are important, but you also need to be able to multiply values together. In most computer languages, the * symbol is used to indicate multiplication, not the x symbol. You might be able to use x on paper, but to the computer, x is a variable, not a multiplication symbol. If you enter the command
    Code:
    Result = 3 x 2
    the interpreter will give you a syntax error. Rather, you should enter the command
    Code:
    Result = 3 * 2
    to be correct.

    Division (/ and \)



    The division operator is the last of the four commonly used arithmetic operators. Among the common arithmetic operators, division is the most complicated arithmetic operation a computer performs. VBScript has two types of division operators. The first operator handles numbers with decimal points. Usually referred to as the floating-point division operator, it's represented by the / symbol If you are relatively new to programming, you might be wondering at this point if you can use a more familiar symbol for the same purpose. The answer is no; you cannot use the familiar Þ symbol in computer speak-you must instead use the / symbol for floating-point division. The floating-point division operator is designed to divide values with decimal points, but it can also divide numbers without decimals. The syntax for division is the same as any of the other operators presented so far:

    Code:
    c = a / b
    This code divides the variable b into a and puts the result into the variable c. Similarly, you could use numbers and perform a division such as
    Code:
    c = a / 2
    which, in this case, divides the variable a in half. If the variable a were set to some valid numeric value, say 3, the result stored in c would be 1.5.

    The integer division operator essentially tells VBScript to return a non-decimal result. \ is used for Integer Division. Integer division is performed the same way floating-point division is, but the operator is different. The following code shows the syntax:
    Code:
    Result = a \ b

    Exponents (^)



    The exponent operator calculates a result based on the following formula:

    result = number ^ exponent

    The expression
    Code:
    Result = 2 ^ 3
    evaluates to 8 because 2 * 2 * 2 = 8. The exponent function, which is available on most scientific and business calculators, is often used in formulas relating to science, engineering, and accounting.

    Modulo Arithmetic (Mod)



    The Mod function is another powerful arithmetic operator. Essentially, the Mod function returns the remainder after dividing one number into another. The syntax for the function is
    Code:
    Result = a Mod b

    Negation (-)



    The last of the arithmetic operators is the negation operator. Simply put, this operator changes the sign of a value contained in a variable or creates a negative number. For instance, the expression
    Code:
    Result = -2 * 3 
    results in assigning the value -6 to the variable Result.

    Arithmetic Operator Precedence



    Now that you have seen all the arithmetic operators that VBScript has to offer, you might be wondering how all the operators behave when put together in a single line of code. For instance, is the following line of code
    Code:
    2 * 3 + 6 / 2
    equal to 9 or 6? When you combine arithmetic operators in an expression, you need to know the order of precedence of the operators. That is, what operations will the computer execute first as it moves from left to right across the expression?
    Code:
    +-------+-------------------------------------------+
    | Order |                 Operation                 |
    +-------+-------------------------------------------+
    |   1   | Exponents (^)                             |
    |   2   | Negation (-)                              |
    |   3   | Multiplication (*) and division (/ and \) |
    |   4   | Modulo arithmetic (Mod)                   |
    |   5   | Addition (+) and subtraction (-)          |
    |   6   | String concatenation (&)                  |
    +-------+-------------------------------------------+
    Notice that multiplication and division are on the same level of precedence. In this case, multiplication and division are executed in left-to-right order when it's their turn in the order of precedence. The expression
    Code:
    2 * 3 + 6 / 2
    has three arithmetic operators: multiplication, addition, and division. As you can see in this table, multiplication is the first operation performed in the expression as you move from left to right. Both multiplication and division have the same order of precedence, so they are handled on a first-come, first-served basis. Because the multiplication is first, it gets executed first. As a result, the first operation in the expression is 2 * 3, which equals 6. This reduces the expression to
    Code:
    6 + 6 / 2
    That leaves an addition operator and a division operator in the expression. .The expression 6 / 2 equals 3, so the expression now becomes
    Code:
    6 + 3
    Because only one operator remains, the choice is obvious-the result equals 9. VBScript follows this process of ordering an expression of multiple operators every time. When you are writing code, you must keep this order in mind.

    One good way to maintain the order of expressions when writing and maintaining code is to designate sub expressions in parentheses. You can use parentheses freely in expressions, and they ensure that the sub expression will be fully evaluated first before it is evaluated as part of the larger expression. For example, you could write
    Code:
    c = 2 * 3 + 6 / 2 
    and c would evaluate to 9, as figured previously. You could also code the same expression as
    Code:
    c = (2 * 3) + 6 / 2 
    or
    Code:
    c = 2 * 3 + (6 / 2) 
    or even
    Code:
    c = (2 * 3) + (6 / 2)
    and the results would still be the same: 9. However, with these statements, particularly the last one, the expression is much easier for a programmer to read and understand.

    When using the parentheses approach, you must be aware of how subexpressions are evaluated. If you coded the statement as
    Code:
    c = (2 * 3 + 6) / 2
    you get a result of 12 / 2 = 6 stored in c. The subexpression always gets evaluated first, which in turn affects the results of the overall expression. Because the human mind can digest information more easily by breaking it down into smaller pieces, subexpressions will make your code easier to follow and probably less error prone as well.

    Comparison Operators



    The first set of operators VBScript provides are arithmetic operators. This section discusses the second type: comparison operators. As the name implies, you use comparison operators to compare one or more variables, numbers, constants, or a combination of the three. VBScript has many different types of comparison operators, and each checks for a different comparison condition.

    Equality (=)



    You use the equality operator to see if a variable, constant, or number is equal to another. It's common to mistake the equality operator for the assignment operator, which is also represented by an equal sign. You use the assignment operator to set a variable equal to another variable, number, string, constant, or other data entity. For example, the statement
    Code:
    a = b
    assigns the value contained in b to the variable a.

    The equality operator, on the other hand, is used to test whether one value is equal to another. The syntax for the equality operator looks similar to that for the assignment operator:
    Code:
    a = b
    where a and b can be variables, constants, or numbers. It is the context in which you use the expression that determines whether it is treated as an assignment or an equality check. Equality is always used in the context of checking a condition. For example, a statement

    such as:
    Code:
    if a = b then 
    is an example of the equality operator because it is a conditional check. As a rule of thumb, you can assume that if a = b appears in a statement by itself, it is an assignment statement. If you see a = b as part of any other expression, it is used in the equality context.

    Inequality (<>)



    Another important comparison operator is the inequality operator. You use this operator to test whether a variable is not equal to another variable or some data element. The syntax for the inequality operator is
    Code:
    a <> b 
    where a and b are variables, constants, strings, or numbers.

    Less Than and Greater Than (< and >)

    You might have a condition where you don't care whether a variable is equal to another, but you do want to know whether it is greater than or less than another variable, number, or constant. In such a case, you need the greater-than and less-than operators. The syntax for these two operators is
    Code:
    a > b 
    and
    Code:
    a < b
    where a and b are variables, constants, numbers, or strings, and the result is True if the expression is true.

    Less Than or Equal To and Greater Than or Equal To (<= and >=)

    Sometimes, you also might want to see whether a variable is greater than or equal to some other variable, constant, or number. Perhaps you want to know if it is less than or equal to the entity. Then, you can combine operators to use the less-than-or-equal and greater-than-or-equal operators, <= and >=. The syntax for these operators is
    Code:
    a <= b
    and
    Code:
    a >= b

    Object Equivalence (Is)



    The syntax of the Is operator is

    result = object_reference1 Is object_reference2

    where object_reference1 and object_reference2 are references to objects and result is either True or False, depending on whether the statement is true.

    This operator does not compare one object to another, nor does it compare values. This special operator simply checks to see if the two object references in the expression refer to the same object.

    Comparison Operator Precedence



    Unlike arithmetic operators, comparison operators do not have a specific order of precedence. They are simply executed as they are found from left to right across an expression. When VBScript encounters an expression such as
    Code:
    If 3 > 5 < 10 Then 
    VBScript will evaluate the conditions in the order they appear from left to right. In this case, the part of the expression that comes first, 3 > 5, is evaluated first as False. False in VBScript is equivalent to 0, so the expression becomes
    Code:
    If 0 < 10 Then 
    Because 10 is greater than 0, the expression is True.

    Logical Operators



    The last category of operators in VBScript is logical operators. The logical operators might require a more significant amount of understanding to appreciate. Logical operators use the binary number system to represent information. The part of the binary number system used in this lesson is very simple: Expressions evaluate to True or False, respectively. A logical expression, such as
    Code:
    5 > 3
    is a true statement and would return True if evaluated. If you inspect the value of True, you would see that it contains the true indicator of -1. It turns out that -1 is a convenient way to represent True internally in the binary system upon which your computer and VBScript are based.

    Negation (Not)



    The first operator is called the negation operator. This operator has the following syntax:

    result = Not expression

    Following is the result variable in relation to expression.

    Negation (Not) results.



    If expression is… Then result is…
    Code:
    +-----------+-----------+
    |Expression | Result    |
    +-----------+-----------+
    | True      | False     |
    | False     | True      |
    | Null      | Null      |
    +-----------+-----------+
    If you enter the expression
    Code:
    a = Not 1 > 2 
    where the expression is 1 > 2, the result is stored in the variable a. The value stored in variable a will be True. The expression 1 > 2 is false because 1 is not greater than 2. Not simply flips a false over to true or a true over to false. Because the expression is false, the Not operator makes the result true. As a result, the VBScript value for true, True or -1, will be stored in variable a. If the expression is null, the Not operator will return a null when applied to the expression.

    Conjunction (And)



    The conjunction operator compares two or more variables in some type of test. The syntax for the conjunction operator is

    result = expression1 And expression2

    Following is the result variable in relation to expression1 and expression2.

    If expression1 is… and expression2 is… then result is…
    Code:
    +-------------+-------------+--------+
    | Expression1 | Expression2 | Result |
    +-------------+-------------+--------+
    | True        | True        | True   |
    | True        | False       | False  |
    | False       | True        | False  |
    | False       | False       | False  |
    +-------------+-------------+--------+
    In order to obtain True in the result variable, both expression1 and expression2 must be true. You often use this operator to make sure two or more conditions are true before performing some action.

    Disjunction (Or)



    Another frequently used logical operator is the disjunction operator. This operator has the same syntax as the conjunction operator:

    result = expression1 Or expression2

    Following is the result variable in relation to expression1 and expression2.

    Disjunction (Or) results.

    If expression1 is… and expression2 is… then result is…
    Code:
    +-------------+-------------+--------+
    | Expression1 | Expression2 | Result |
    +-------------+-------------+--------+
    | True        | True        | True   |
    | True        | False       | True   |
    | False	      | True        | True   |
    | False       | False       | False  |
    +-------------+-------------+--------+
    This operator behaves quite a bit differently from the conjunction operator! In this case, any of the expressions can be true for the result to be true. The result is false only if all the expressions are false. You typically use this operator when you have to make a decision where any of a number of activities could occur, but only one must occur for the operation to proceed.

    Exclusion (Xor)



    The exclusion operator is another in the family of logical operators that you can use to make decisions based on the contents of one or more expressions. It checks whether one and only one condition is exclusively true. The syntax of the exclusion operator is

    result = expression1 Xor expression2

    Following is the result variable in relation to expression1 and expression2.

    Exclusion (Xor) results.



    If expression1 is… and expression2 is… then result is…
    Code:
    +-------------+-------------+--------+
    | Expression1 | Expression2 | Result |
    +-------------+-------------+--------+
    | True        | True        | False  |
    | True        | False       | True   |
    | False       | True        | True   |
    | False       | False       | False  |
    +-------------+-------------+--------+

    Logical Equivalence (Eqv) and Implication (Imp)



    The remaining two logical operators are Eqv and Imp. The equivalence operator checks whether two expressions are bitwise equivalent to each other. The syntax is

    result = expression1 Eqv expression2

    The Imp operator performs a logical implication on two expressions. The syntax for that operation is

    result = expression1 Imp expression2

    If you're a very experienced programmer, you know what Imp and Eqv do, and you're probably glad to see them in the VBScript . However, if you're a beginner, you might be thinking at this moment, "What on earth is bitwise equivalence and logical implication?"

    Like all the logical operators covered here, the Imp and Eqv comparisons are performed on a bit-by-bit basis and the results are set on a bit-by-bit basis. For the other operators, the functionality it provides and the expected results are intuitively obvious. For Imp and Eqv, the results are less intuitive. Of course, any expression is ultimately represented in bits on a computer, and to understand these operators, you must think in terms of these bits.

    If expression1 is… and expression2 is… then result is…
    Code:
    +-------------+-------------+--------+
    | Expression1 | Expression2 | Result |
    +-------------+-------------+--------+
    | True        | True        | True   |
    | True        | False       | False  |
    | False       | True        | False  |
    | False       | False       | True   |
    +-------------+-------------+--------+

    Implication (Imp) results.



    If expression1 is… and expression2 is… then result is…
    Code:
    +-------------+-------------+--------+
    | Expression1 | Expression2 | Result |
    +-------------+-------------+--------+
    | True        | True        | True   |
    | True        | False       | False  |
    | False       | True        | True   |
    | False       | False       | True   |
    +-------------+-------------+--------+
    Most types of programs use these operations infrequently, so they are typically known to advanced programmers.

    Logical Operator Precedence

    Like arithmetic operators, logical operators have an order of precedence.
    Code:
    +-------+-------------------+
    | Order | Operation         |
    +-------+-------------------+
    | 1     | Negation (Not)    |
    | 2     | Conjunction (And) |
    | 3     | Disjunction (Or)  |
    | 4     | Exclusion (Xor)   |
    | 5     | Equivalence (Eqv) |
    | 6     | Implication (Imp) |
    +-------+-------------------+
    Any type of operator is evaluated from left to right in an expression. For an example of logical operator precedence, consider the following code statement:
    Code:
    If a Xor b And c Or d Then
    For this example, suppose a is true, b is false, c is false, and d is false. Moving from left to right, you have the operators Xor, And, and Or. Because And gets first precedence, the expression b And c is evaluated first. The result of False And False is False. As a result, the expression becomes
    Code:
    If a Xor False or d Then
    Now, moving from left to right, the two operators are Xor and Or. Or is next in the line of precedence, so the next expression up for evaluation is False Or d. Because d is false, False Or False equals False. The expression becomes
    Code:
    If a Xor False Then 
    Now, Xor is the only operator left. Because a is true, True Xor False is True. As a result, the expression reduces to
    Code:
    If True Then 
    which means the code that follows the if-then statement will be executed. It is very important to understand the order of precedence when you have more than one expression connected together in a single statement.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice