Article

ASP Language Basics

Page: 1 2 3 4 5 6 Next

Operators

As I said above, any program that does anything useful will likely have to perform some kind of calculation, or manipulate information in some way or another. So far we've seen how to define pieces of information and store them in variables in preparation for that work to be done. In this section, I'll introduce some of VBScript's operators. Using operators you can take simple values (like numbers and strings of text) and perform operations on them (like addition, subtraction, or concatenation).

Let me show you a few simple examples so you know what I mean:

Dim intA, intB, intC ' Three integer variables  
intA = 10            ' Assigns value 10 to intA  
intB = 10 + 1        ' Assigns value 11 to intB (+ is addition)  
intC = intB โ€“ intA   ' Assigns value 1 to intC (- is subtraction)  
intA = intB * 3      ' Assigns value 33 to intA (* is multiplication)  
intC = intA / intB   ' Assigns value 3 to intC (/ is division)

Notice how I used the symbols +, -, *, and / to perform addition, subtraction, multiplication, and division respectively. These symbols are arithmetic operators. Notice also that operators may be used on literal values (10 + 1), variables (intB โ€“ intA), or both (intB * 3).

Other arithmetic operators that are less commonly used include the modulus operator (MOD) and the exponentiation operator (^):

intC = intB MOD 3    ' Assigns value 2 to intC (MOD is modulus op.)  
intB = intC ^ 2      ' Assigns value 4 to intB (^ is exp/power op.)

The modulus operator gives the remainder of an integer division. In the first example above, the value stored in intB (11) divided by three is 9 with a remainder of 2 โ€“ this remainder is the value calculated by the modulus operator. On the second line, the value stored in intA (33) divided by three is 11 with a remainder of 0, which is the value resulting from the modulus operation.

The exponentiation, or power operator is used to raise a number to some power. In this case, we're raising the value stored in intC (2) to the second power. In other words, we are squaring 2 to obtain 4, and this is the value resulting from the exponentiation operator.

You can perform multiple operations in a single statement, if you want to. Note that arithmetic operations obey the same rules of precedence as they do on a scientific calculator: multiplication and division first, followed by addition and subtraction. Of course, we can use parentheses to change this if we want:

intA = 1 + 2 * 3     ' intA = 7  
intB = (1 + 2) * 3   ' intB = 9

Recall that variables don't have to contain numbers. They can also hold strings of text; and operations can be performed on strings as well:

Dim strName  
strName = "Kevin"  
Response.Write "Hi " & strName & ", how are you today?"

The code above displays the message Hi Kevin, how are you today? Notice how the & operator is used to stick strings of text together (in other words, concatenate strings). For obvious reasons, & is called the string concatenation operator. Notice also that we've used the result of the expression "Hi " & strName & ", how are you today?" as the value to be printed out by the Response.Write command. Nothing says we must store the result of an arithmetic calculation or other operation in a variable; as the example above shows, if we only need the value in one place we can simply type the expression where we need it. Finally, note the new variable name prefix str, which I use to indicate a variable containing a string of text.

Another family of operators is called the comparison operators. As their name suggests, these operators are used to compare different values together. While the arithmetic operators produce numbers and the string concatenation operator produces text strings, the comparison operators produce Boolean values (i.e. either true or false). As before, some examples should illustrate this effectively:

' From above: intA = 7, intB = 9, and intC = 2  
 
Dim blnD, blnE, blnF    ' Three more variables  
blnD = (intA = 7)       ' blnD = True (= tests equality)  
blnE = (intA = intB)    ' blnE = False (intA doesn't equal intB)  
blnF = (intA <> intB)   ' blnF = True (<> tests inequality)  
blnD = (intA > 0)       ' blnD = True (intA is greater than 0)  
blnE = (intB < 6)       ' blnE = False (intB is not less than 6)  
blnF = (intC >= 2)      ' blnF = True (intC is greater/equal to 2)  
blnD = (intC <= 1)      ' blnD = False (intC isn't lesser/equal to 1)

As shown here, the comparison operators are =, <>, >, <, >=, and <=. Astute readers may have noticed that '=' plays a dual role in VBScript, acting as either the assignment operator or the comparison for equality operator, depending on the context in which it is used. '=' and '<>' may also be used to check if two variables contain the same or different string values, respectively. I use the bln prefix for the variables' names to indicate that they will contain Boolean values (True or False).

You might be wondering about the practical uses of these comparison operators. In fact, they are essential when it comes to making your program do different things under different conditions. To accomplish such a feat, however, you'll need one more tool in your belt: control structures.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links