Operators & Expressions in ASP
You are already familiar with some operators in JScript, even if you've never used a programming language before. In fact, you've already used some of them in the previous chapters, and they didn't look unusual at all to you.
The arithmetic operators are quite what you expect them to be, similar to the ones used in the basic arithmetic you learned in the first grade:
+ for addition
- for subtraction
* for multiplication
/ for division
% for modulus
++ increment
– decrement
The increment and decrement operators may seem a little stranger at first sight: they are used to increase, respectively decrease a variable by one. So, when you write
x = 1
x++
this means that x equals 2
You will see a lot more of these operators when we discuss the conditional structures and the loops below.
Note that the "+" sign can also be used as a string operator – for instance, when you need to put two strings of text together. Begin by assigning the text to a variable, and then use the "+" operator to bring them together:
text = "The capital city is ";
capital_city = "Washington";
text_complete = text + capital_city;
Now the text_complete variable contains the string "The capital city is Washington". Make sure you include the blank spaces in the string, or, if you want to use them separately, place them between quotation marks:
text = text1 + " " + text2 + " " + text3;
JScript also has assignment operators. You've used one of them to assign values to variables, and it probably "felt" quite normal:
x = 5;
meaning that you've assigned the value 5 to the variable x.
JScript also uses the compound assignment operators, which assign a value after performing the designed operation: x += y is the same as x = x + y. If both x and y are numeric or boolean, then they will be added, is both are strings, or only one of them is a string, they will be concatenated.
In the same way, you have:
x -= y the same as x = x – y
x += y the same as x = x * y
x /= y the same as x = x / y
x %= y the same as x = x % y
When you need to find out if x is equal to 5, you'll use one of the comparison operators: ==. So, x == 5 will return either "true" or "false". The comparison operators, all of whom return the values "true" or "false", as the following:
== for "is equal to"
!= for "is not equal to"
> for "is greater than"
< for "is less than"
>= for "is greater than or equal to"
<= for "is less than or equal to"
Finally, you have the logical operators. Again, you've probably seen these ones before as well:
! for "not"
&& for "end"
|| for "or"
Also like in regular arithmetic, parentheses are used to alter the order in which the operations are performed, meaning that the operations between brackets have the priority.
Considering the variables:
x = 1
y = 2
the comparison
x == y
will return "false", but the comparison
!(x == y) will return "true.
Considering the same variables, (x == 1 || y == 5) returns true.
Translated into plain English, this sounds like this: If x is equal to 1, and y is equal to 2, than the statement "Either x equals one OR y equals 5" is true. Learning a programming language is a lot like learning a foreign language (a bit easier, though), so, when you tell the computer do to something, make sure you know exactly what you want to say, and then translate it into the respective programming language. Usually, there is more than one way for telling the computer to do the same thing, same is, in the English language, there are several sentences with the same meaning, and you decide which one you want to use.
So, when you "translate" from English into JScript, "if both x equals 1 AND y equals 5 " will look like (x == 1 && y == 5), and will return "false" (both in plain English and in JScript).
Other posts in asp
- What is ASP?
- Installing ASP
- First ASP Script
- JScript Syntax
- JScript Data Types
- Variables in JScript
- Arrays in JScript
- Conditional Structure in JScript
- Loop Structures in JScript
- Functions in JScript
- Object Oriented Programming in ASP
- Classes in ASP
- Response and Request in ASP
- File Manipulation in ASP
- Cookies in ASP
- Sessions in ASP
- Database Manipulation in ASP
- Debugging & Efficiency in ASP
- Beyond ASP
No comments yet. Be the first.
Leave a reply