Lesson 5 | ASP Arithmetic Operators |
Objective | Create formulas and test constants and variables. |
Create Formulas using ASP Arithmetic Operators
How do I create formulas, test constants and variables using ASP Arithmetic Operators?
Arithmetic operators in Active Server Pages (ASP) allow developers to perform mathematical calculations and create formulas using constants and variables. The basic arithmetic operators in ASP include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%), which returns the remainder of a division operation.
Here's an overview of how you can use these operators:
Dim x
Dim y
Assigning Values:
Next, you can assign values to the variables. These can be constant values or the results of calculations.
x = 5
y = 10
Creating Formulas:
Now, you can create formulas using these variables and the arithmetic operators:
Dim sum
sum = x + y 'Addition
Dim difference
difference = y - x 'Subtraction
Dim product
product = x * y 'Multiplication
Dim quotient
quotient = y / x 'Division
Dim remainder
remainder = y Mod x 'Modulus
Each of these formulas performs a different mathematical operation on the variables x and y, and stores the result in a new variable.
Testing Constants and Variables:
You can also directly use constants in your formulas:
Dim square
square = 5 * 5 'This squares the number 5
Or mix constants and variables:
Dim tripled
tripled = x * 3 'This triples the value of x
Outputting Results:
You can use the Response.Write method to output the results of these operations to the HTML document:
Response.Write("Sum: " & sum & "<br>")
Response.Write("Difference: " & difference & "<br>")
Response.Write("Product: " & product & "<br>")
Response.Write("Quotient: " & quotient & "<br>")
Response.Write("Remainder: " & remainder & "<br>")
Response.Write("Square: " & square & "<br>")
Response.Write("Tripled: " & tripled & "<br>")
The "&" operator is used to concatenate strings in ASP, and the "
" HTML tag is used to create a line break between each result.
Remember to use the ' symbol for making comments in your ASP code to make it more understandable. Comments do not affect the execution of the script, they're only for developers to provide explanations or to temporarily disable certain lines of code.
Mathematical Operators
Sooner or later, in a Web application, you will need to perform mathematical calculations. In a previous lesson, we used the multiplication (*
) and subtraction (-
) operations to perform a discount. Here are other mathematical operators available for use in ASP code:
+
Addition 1 + 2 = 3
-
Subtraction 2 - 1 = 1
*
Multiplication 6 * 3 = 18
/
Division 6 / 3 = 2
\
Integer Division 6.3 \ 3.1 = 2
Mod
Modulus 8 Mod 3 = 2 (Any remainder is discarded)
^
Exponentiation 8 ^ 2 = 64
(NOTE: This type of division rounds the two numbers to integer values then produces an integer result. Any remaining decimal values are discarded.)
Logical -Boolean-operators
There are also a number of logical operators available for our use.
Two of the most commonly used are
- NOT and
- AND.
The next lesson presents some common programming structures used in ASP scripts.
If Not (A = 5) Then . . .
'If A is NOT equal to 5,
'then something happens
'Can also be written as A <> 5
.
.
If (A <> 5) AND (A <> 7) Then . . .
'If A is not equal to 5 AND
'A is not equal to 7
'then something happens
.
ASP Operators Quiz
Click the Quiz link below to check your understanding of the topics discussed in the last few lessons.
ASP Operators Quiz