Arithmetic operators in ASP

An operator is simply a method implemented in a programming language to modify, replace, or combine values represented by variables. Without operators, a programming language would not be very useful because the programmer would not be able to store or manipulate the data. The arithmetic operators manipulate numerical values and the assignment operator stores a value to a variable.

Assignment Operator

As the name implies, an assignment operator (or = ) assigns the right hand side expression or a value to a variable on the left hand side. For example, the following

strName = "John Smith"
intWeight = 170
dtJohnBirthDate = #9/15/1985#

assigns the value "John Smith" to the variable strName. Remember strings are closed inside of double quotation marks. The numerical value 170 is assigned to the variable intWeight. A date variable dtJohnBirthDate is assigned the date value 9/15/1985. The date expression is surrounded by the pound signs to distinguish it from division of 9 divided by 15 and this result then divided by 1985.

The following are examples of an assignment with an expression on the right hand side:

floatAmountDueBeforeTax = intItems * floatCostPerItem
strName = "B" + "o" + "b" + " Smith"
intFour = intTwo + intTwo

The variable floatAmountDueBeforeTax is assigned the result of the expression on the right side of the assignment operator. In the context of string expressions, the "+" operator is referred to as the concatenation (or "join") operator. It simply joins or concatenates the string expressions. The strName is assigned the string "Bob Smith" with "B" + "o" + "b" + " Smith". When the "+" operator is used with numerical expressions, it is referred to as the addition operator. This "addition" operator simply adds the numerical expressions on the right hand side of the assignment operator. In intFour = intTwo + intTwo, intFour is assigned the result of adding intTwo to intTwo.

There are a few things to note when using an assignment operator:

  1. Before assigning a new value to a variable, always make sure that you no longer need the old or the original value.
  2. When assigning values or calculated expressions, always make sure the left hand side is a variable. The following
    "strName" = "John Smith"
    70 = 137
    #1/5/1985# = #9/15/1985#
    are not valid assignments because the right side is not a variable.

Arithmetic Operators

The arithmetic operators are also referred to as mathematical operators. The mathematical operators are used to manipulate the values that the variables hold. Each programming language uses different syntax to represent mathematical operators and VBScript is no exception. The most common arithmetic operators include: addition, subtraction, multiplication, division, and modulus.

Addition Operator

The addition operator simply adds one variable or a numerical expression to another variable or a numerical expression. The general syntax of using an addition operator is

intResult = argument + argument

The addition operator has precedence over the assignment operator. That means first the addition will be performed before the assignment takes place. For example,

intFour = (intOne + intOne) + 2

the variable intOne (it is assumed numerical value is 1) is added first to intOne and then to intFour; then, this result is added to 2. Although the use of parenthesis is not necessary in the statement, their use helps to clarify the precedence or order of execution. The final result (or the number 4) is assigned to the variable intFour by the assignment operator.

Let's do an example that prints different sizes of text using the heading tag with the addition operator:

<%
DIM intHeadingCounter
intHeadingCounter = 1
Response.Write "<h" & intHeadingCounter & ">Heading 1</h" & intHeadingCounter & ">"
intHeadingCounter = intHeadingCounter + 1
Response.Write "<h" & intHeadingCounter & ">Heading 2</h" & intHeadingCounter & ">"
intHeadingCounter = intHeadingCounter + 1
Response.Write "<h" & intHeadingCounter & ">Heading 3</h" & intHeadingCounter & ">"
intHeadingCounter = intHeadingCounter + 1
Response.Write "<h" & intHeadingCounter & ">Heading 4</h" & intHeadingCounter & ">"
intHeadingCounter = intHeadingCounter + 1
Response.Write "<h" & intHeadingCounter & ">Heading 5</h" & intHeadingCounter & ">"
intHeadingCounter = intHeadingCounter + 1
Response.Write "<h" & intHeadingCounter & ">Heading 6</h" & intHeadingCounter & ">"
%>

In this example, we use the addition operator to print the text in different sizes. We start by declaring a variable called intHeadingCounter and then we initialize it to 1 by using the assignment operator. We next print Heading 1 using the <h1> tag:

"<h" & intHeadingCounter & ">Heading 1</h" & intHeadingCounter & ">"

is equivalent to

<h1>Heading 1</h1>

The ampersand (&) with the response.write object joins our strings or HTML tags. To print heading level 2, we add one to intHeadingCounter and then print again with response.write but this time we use <h2> instead of <h1>. After adding 1 each time a different heading is printed, the remaining code prints the heading 3, 4, 5, and 6. The following shows the output of the code:

		

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Subtraction operator

With the subtraction operator (or "-"), you can subtract one argument from another. Unlike addition operator, the subtraction operator can produce different results depending on the order of arguments. For instance,

intTwelve = 5 + 7
intTwelve = 7 + 5

is 12. However, if you change arguments when you use the subtraction operator, the result can be different. For example,

intPositiveTwo = 7 - 5
intNegativeTwo = 5 - 7

in both statements we use the same numerical values and the same operator yet the result is different.

Multiplication operator

The multiplication operator can be used to multiply one numerical value with another. The symbol for multiplication in VBScript is the asterisk ("*"). The following

intThirtyFive = 7 * 5

multiplies 7 with 5 and assigns the result (or 35) to the variable intThirtyFive. Because multiplication has higher precedence than addition and subtraction, use of parentheses can enforce the order of execution. For example,

intTwenty = 5 * 2 + 10
intSixty = 5 * (2 + 10)

the variable intTwenty is assigned the value 20 because multiplication is performed first (5 * 2 = 10) and addition is performed next (10 + 10). In the second statement, the addition is performed first, resulting in 12 and then multiplication with 5; produces 60.

Division operator

VBScript has two divisions: integer and float. For each of these operations, a different symbol is used. For integer division, the backslash ("\") is used. For example,

intFive = 10 \ 2
intZero = 0 \ 10
intFour = 9 \ 2

each of these expressions returns an integer value. The integer division rounds down a floating number. For example, the 9 \ 2 produces 4; similarly, 1 \ 3 produces 0.

For those expressions that require the fractional part, the float division operator (the forward slash: "/") should be used. For example,

floatFourAndHalf = 9 / 2
floatResult = 10 / 3

each of these expressions returns the fractional part of the resulting value. When working with floats, the Round function can be used to obtain a specific number of decimal places. Depending on the system, the value of floatResult above can vary. To round a floating point number to 2 decimal places, use Round (.333333333333333, 2), this will return .33.

When working with division, be careful not to divide by zero because this is undefined and will cause an error. When there are number of division operators used in an expression, the division starts from right to left. The division and multiplication have the same degree of precedence. When these both operators are used in an expression, the expression is evaluated from left to right.

Modulus operator

The division can return an integer or a floating-point value, what about the remainder? The modulus operator returns the remainder after division. The keyword "MOD" operator is used to return the remainder. For example,

9 MOD 2 returns 1
15 MOD 4 returns 3
0 MOD 4 returns 4
4 MOD 2 returns 0

Operator precedence

To correctly formulate expressions, it is very important to learn the order of operator execution. The following examples emphasize the need for understanding the order of execution:

4 + 2 * 3 = ?
2 + 2 * 3 - 1 \ 1 + 4 = ?

In the first statement, the answer is 10 (2 * 3 = 6 + 4 = 10) not 18 (4 + 2 = 6 * 3 = 18). Because multiplication and division has precedence over addition and subtraction, 2 is first multiplied with 3 and then the result is added to 4. For clarification, the statement can be rewritten exactly as it is executed as 4 + (2 * 3) = 10. Because parentheses have the highest order of precedence, they are executed first.

What is evaluated first when both division and multiplication operators are used in a statement, as in second statement? The answer depends on the location of their placements in a statement. When more than one of or more of multiplication or division operators are used, the execution begins from right to left. In comparison, both addition and subtraction operators are evaluated from left to right in the order they are used.

Let's do the second statement step by step:

1 \ 1 = 1 (because division has the highest precedence and execution is began from right)
2 * 3 = 6 (next multiplication has precedence over other operators)
Now the original equation can be represented with substitution of the above calculations:
2 + 6 - 1 + 4 = ?
Finally, we execute the above statement from right to left and obtain 11

The second statement can be rewritten exactly the way it is executed: 2 + (2 * 3) - (1 \ 1) + 4 = 11. Again, code inside the parenthesis is executed first.

Let's verify our results by coding our examples in VBScript:

<%
Response.Write "4 + 2 * 3 = " & 4 + 2 * 3
Response.Write "
2 + 2 * 3 - 1 \ 1 + 4 = " & 2 + 2 * 3 - 1 \ 1 + 4

%>

We use a response.write statement to print the result of each expression. As the following output shows:

4 + 2 * 3 = 10
2 + 2 * 3 - 1 \ 1 + 4 = 11

the first expression equals 10 and the second expression equals 11, as we expected.

Table 1 summarizes order of precedence for the arithmetic operators shown above.

Table 1 arithmetic operators's order of precedence
Highest (executed first)  
code in parentheses
Multiplication, Division (*, /)
Integer Division (\)
Modulus (MOD)
Addition, Subtraction (+, -)
String Concatenation (&)
Lowest (executed last)