Computers & ProgrammingASPBackend Development

Working with Operators in 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.

Operator Description Example Results
+ Addition x = 5 + 1 6
- Subtraction x = 5 - 1 4
* Multiplication x = 5 * 1 5
/ Division x = 6 / 3.5 1.17428571428571
^ Exponential x = 3 ^ 3 27
Mod Modulus x = 10 Mod 3 1
- Negate x = -5 -5
\ Integer Division x = 6 \ 3.5 1

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.

Operator Description Example Results
= Equal To 5 = 1 FALSE
< Less Than 5 < 1 FALSE
> Greater Than 5 > 1 TRUE
<= Less Than Or Equal To 5 <= 1 FALSE
>= Greater Than Or Equal To 5 >= 1 TRUE
<> Not Equal To 5 <> 1 TRUE

Logic

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

Operator Description Example Results
And All Must be TRUE True and False FALSE
Or One Must be TRUE True or False TRUE
Not Reverse of TRUE Not True FALSE

Concatenation

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

Operator Description Example Results
& String Concatenation myString = "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>

Leave a Comment

Your email address will not be published.

Scroll to Top