# Understanding Operators

There are many types of operators. There are those who drive
cars, those who perform surgery, those who run casinos, and so on. We don't care
about them, however (at least not for the purposes of this primer!) On the other
hand, we care a lot about those that add or subtract things from each other, and
in other ways manipulate one thing with another. I'm talking about the operators
in programming languages; languages like Perl, VBScript and JavaScript;
languages near and dear to a web developer's heart!

Since operators are so fundamental to the work of programming languages, it is
important to understand the types of operator that there are, and their place in
a language. Understanding fundamental aspects of language structure makes it
simpler to get to know a language you are seeing for the first time. That being
so, a little clarification couldn't hurt. I don't intend to provide a
comprehensive list of either the types of operator or of the operators
themselves, but rather to provide a basic understanding of the concepts
involved.

In a computer language it's the operators that tell the computer what to do. If
you want to add one to one to get a result, you would tell the computer "result
equals one plus one". In this example there are two type of operator at work.
"Plus" is an arithmetic operator that says to add the two numbers either side of
itself together. "Equals" is an operator that says to assign the value of the
answer to "result".

Not all types of operator are present, or implemented, in every language. Perl,
for example, has a few special groups of operators that are peculiar to Perl.
Common to most programming languages, and certainly to those that I have
mentioned here, are arithmetic, assignment, relational and logical.

Arithmetic operators include the add, subtract, multiply and divide basic
building blocks, along with exponentiation, modulus and increment. Hopefully,
you are already familiar with the operation of the first four! It is worth
noting that there are frequently variations on the add and subtract. In addition
to addition, there is also the "Concatenate" operator. Though concatenate is
usually referred to as a different operator type, namely a string operator, it
is sometimes the same symbol as add (e.g. "+" in JavaScript). Using JavaScript
for an example, -- result = 1 + 1 -- would yield a value of 2 in result, while
-- result = "one " + "one" -- would yield a value of "one one" in result.

"Add" adds one number to another, meaning that is operates on two numbers, or
has two operands. Operators with two operands are called "binary" operators.
Those with one operand are called unary, and those with three are called
ternary. I don't know of any operators with more than three operands, but they
might exist in some language.

While subtract is a binary operator, there is also a unary variation. In its
unary form, minus is used to define a negative number, "-1" for example.
Exponentiation refers to raising a number to a power. For example (in Perl) 2**4
is equivalent to 2*2*2*2 which yields 16 (2 to the fourth power). Modulus
(a.k.a. Modulo) returns the remainder after dividing the first operand by the
second operand. For example -- result = 7 modulus 3 -- would give a value of 1
in result.

Assignment operands direct a value into a data item. If our examples above we
have shown "equals" as an assignment operator. Most languages these days,
including the three previously mentioned, assign from right to left. That is, in
the arrangement "op1 = op2" the value already in op2 is assigned to op1,
replacing its value. There are still some languages, however, that assign from
left to right. Cobol is one such, as in "move op1 to op2" or "add 1 to 1 giving
result". By the way, in case you think Cobol is dead, it's worth remembering
that it is estimated that there are still more lines of Cobol code in use today
than any other language. Of course, we don't really care very much because it's
(probably) never used for web site development!

Relational operators compare one thing to another. "Equals" is also a relational
operator, as in the JavaScript example -- if op1 == op2 -- where you notice that
the language syntax calls for two "=" signs to distinguish it from its
assignment cousin. "Greater than", "less than" and the negatives "not equal"
(etc.), are all examples of relational operators. Some languages, including
Perl, consider numeric and string relational operator to be different operator
types.

Logical operators include "and", "or" and "not". These operators are used to
combine other conditions in true/false tests. For an example in JavaScript -- if
((op1 == op2) && (op1 == op3)) -- there are two equality condition tests in the
overall condition, joined by a logical "and". If both test evaluate true, the
overall condition is true, if either or both evaluate false the overall
condition is false. In a logical "or" the overall condition is true is either or
both evaluate true, and false when both are false. Be careful with the (unary
operator) logical "not"!! Sometimes it's simple to see what it means. For a
JavaScript example -- if op1 != op2 -- clearly is looking for inequality between
op1 and op2, but consider the Perl example -- $op1 = !$op2 -- where op1 will be
assigned (the "=" assignment operator) a value of zero if op2 is either null or
equal to zero. Trust me, it can get tricky! Negatives are like that! Consider:
there's not no way to misunderstand this!! Beware also of the bitwise operators
(another type) that also include an "and" an "or" (and a thing called an
"exclusive or") and sometimes a "not". These fellas can also be tricky to
understand and are beyond the scope of this little piece!

Hopefully, this explanation of this part of computer language structure will
help you to operate on your own!