An operator is a symbol used for evaluation of an expression. That definition would make more sense if you knew the meaning of expression and evaluation, right? Keep reading...
is any combination of values, variables, operators or other expressions. An operand
is a variable or literal on either side of an operator. An operator
is a symbol (or sometimes word) used to manipulate expressions.
is the process of determining the outcome, or result. When an expression is said to have evaluated to a value, the process has been completed. In the simple mathematical statement, "2 + 2", the entire statement is an expression, the plus sign is the operator, and the expression evaluates to 4. If I used a different operator as in, "2 - 2", the expression would evaluate to something else. So in another sense, operators are used to manipulate operands.
Operators are classified into several categories: arithmetic, assignment, comparison, logical, string and 3 special operators. For now, focus only on familiarizing yourself with them. We'll study how
they are used more in depth in a later lesson.
used to perform mathematical calculations
% modulus or remainder
used to assign a value to a variable
= left operand equals right operand
a = b
+= left operand equals itself plus right operand
a += b
-= left operand equals itself minus right operand
a -= b
compare two operands for equality
== is equal to
!= is not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
compare two Boolean operands for equality (Boolean value is either true or false):
a == 2 && b == 2
a == 2 || b == 2
+ concatenation operator, combines one string with another
+= concatenation operator, combines additional string to itself
Yes, those operators are the same as the 2 in other categories listed above. What makes them string operators is that they are used with strings. Lessons in the following String Tutorial go into great detail about string operators.
When in doubt, use parenthesis. That's as complex as I'm going get because that's all you need to remember. Rather than trying to remember operator precedence hierarchy (which operands matched with which operators to be evaluated first), using parenthesis insures that the correct result will be returned, especially if you get into a complex operation.
In the following example, I'm going to force precedence (meaning I will force one expression to evaluate before the others) just by placing parenthesis around it.
a + b - (x / y) != g - l
In the above example, the expression to the left of not-equal-to (!=) operator contains a sub expression which must be evaluated first in order to carry out an accurate evaluation. Notice the extra set of parens around x / y. They are forcing x to be divided by y before
being subtracted from a + b.
and for more information on expressions and operators
>>Decision Making: If Statement, Else Clause