Working with Operators in ASP

Tuesday, August 14, 2012

tags ASP

VBScript is very flexible when it comes to storing data in variables. You can add variables that contain numbers together, or even concatenate strings. In ASP, we can group operators into four major categories: Arithmetic, Comparisons, Logic and Concatenation.


Arithmetic

The arithmetic operators used in ASP coding are very similar to other programming languages.

OperatorDescriptionExampleResults
+Additionx = 5 + 16
-Subtractionx = 5 - 14
*Multiplicationx = 5 * 15
/Divisionx = 6 / 3.51.17428571428571
^Exponentialx = 3 ^ 327
ModModulusx = 10 Mod 31
-Negatex = -5-5
\Integer Divisionx = 6 \ 3.51


Comparisons

Comparison operators are often used within If..Then blocks of code (conditional statements). They are helpful when you need to compare two variables and make a decision based on the outcome. The result of a comparison operator is either TRUE or FALSE.

OperatorDescriptionExampleResults
=Equal To5 = 1FALSE
<Less Than5 < 1FALSE
>Greater Than5 > 1TRUE
<=Less Than Or Equal To5 <= 1FALSE
>=Greater Than Or Equal To5 >= 1TRUE
<>Not Equal To5 <> 1TRUE


Logic

A logical operator is used in conditional statements that require more than one set of comparisons.

OperatorDescriptionExampleResults
AndAll Must be TRUETrue and FalseFALSE
OrOne Must be TRUETrue or FalseTRUE
NotReverse of TrueNot TrueFALSE


Concatenation

When working with strings, the "&" operator is used to merge strings together to form a new string.

OperatorDescriptionExampleResults
&String ConcatenationmyString = "John" & "Smith"John Smith


Example

<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>

<%

Dim x,y
x = 5 + 1
y = 1
Response.Write (x & "<br />")

If x > 1 Then
  Response.Write("x is greater than 1!<br />")
Else
  Response.Write("x is not greater than 1!<br />")
End If

If (x > 1) And (y > 1) Then
  Response.Write("Both x and y are greater than 1!<br />")
Else
  Response.Write("Either x or y is not greater than 1!<br />")
End If

Dim firstName
Dim lastName
Dim fullName
firstName = "John"
lastName = "Smith"
fullName = firstName & " " & lastName
Response.write(fullName)

%>

</body>
</html>

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

ASP In A Nutshell Sams Teach Yourself Active Server Pages 3.0 in 21 Days Beginning ASP Databases