COG Operators

Math Operators

Sign Read as Purpose
+"plus"Addition.
*"times"Multiplication.
/"divided by"Real division.
%"modulo"The remainder of a division.

Unary Operators

Sign Read as Purpose
-"negative"Makes a number negative.

Relational Operators

Sign Read as Purpose
=="is equal to"Tests two values for equality.
!="is not equal to"Tests two values for inequality. Notes.
>="is greater than or equal to"Tests to see if the left value is greater than or equal to the right value.
<="is less than or equal to"Tests to see if the left value is less than or equal to the right value.
>"is greater than"Tests to see if the left value is greater than the right value.
<"is less than"Tests to see if the left value is less than the right value.
Relational Operators compare two numbers and return a boolean value.

Logical Operators

Sign Read as Purpose
!"not"Reverses a boolean value.
&&"and"This operator returns a true value if both of the boolean values on each side of the operator are true.
||"or"This operator returns a true value if either of the boolean values on each side of the operator is true.

Bitwise Operators

Sign Name Purpose
&Bitwise AndTests the bits of two numbers and returns a number with only the bits that both numbers had.
|Bitwise OrTests the bits of two numbers and returns a number with the bits that either number had.
^Exclusive Or (XOR)Tests the bits of two numbers and returns a number with the bits that only one number had.


Assignment Operators

Sign Read as Purpose
="is assigned to"Assigns the variable on the left to the value on the right.

Operator Precedence and Associativity

Precedence Associativity Category
( ) [ ]left to rightFunction Call, Parentheses, Array Index
-right to leftUnary Negation
* / %left to rightMultiplication
+ -left to rightAddition
>= <= < >left to rightRelational Operators
== !=left to rightEquality Operators
!right to leftLogical "not"
&left to rightBitwise "and"
^left to rightExclusive "or" (XOR)
|left to rightBitwise "or"
&&left to rightLogical "and"
||left to rightLogical "or"
=right to leftAssignment
,left to rightComma
Operators are listed in order of descending precedence.


Precedence Information

The precedence of an operator determines which operations will be evaluated first. As in mathematics, parentheses raise the precedence of part of an expression. An example:

  5 * 5 + 5 = 30

Because multiplication has a higher precedence than addition, 5*5 will be performed before 5+5. If you want the addition to be done first, you need to use parentheses to raise the precedence of part of the expression:

  5 * (5 + 5) = 50

In this case, 5+5 was performed before the multiplication and the result was different.

Associativity Information

The associativity of an operator is the right-to-left or left-to-right direction that an operator works in. An example:

  var=-5 * 5 * 5;

Three operators are used there. The negation, because of its precedence, is performed first. Since its associativity is right-to-left, it affects the 5 to its right. Because the multiplication operators are of the same precedence, their associativity is used to determine which operation is done first. And since their associativity is left-to-right, the -5 * 5 operation is done next. This next example uses parentheses to show the order in which the first example was evaluated:

  var=(((-5) * 5) * 5);

The parentheses do not change the expression here, they only show the order in which the expression would have been evaluated. The last operation to be performed is the assignment. Since it has the lowest precedence it is done last. And because it has right-to-left associativity, it assigns the resulting value on the right to the variable on the left.

  • Create:
This page was last modified 17:41, 23 April 2006.   This page has been accessed 837 times.