
Reference |
| Sign | Read as | Purpose |
|---|---|---|
| + | "plus" | Addition. |
| * | "times" | Multiplication. |
| / | "divided by" | Real division. |
| % | "modulo" | The remainder of a division. |
| Sign | Read as | Purpose |
|---|---|---|
| - | "negative" | Makes a number negative. |
| 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. | ||
| 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. |
| Sign | Name | Purpose |
|---|---|---|
| & | Bitwise And | Tests the bits of two numbers and returns a number with only the bits that both numbers had. |
| | | Bitwise Or | Tests 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. |
| Sign | Read as | Purpose |
|---|---|---|
| = | "is assigned to" | Assigns the variable on the left to the value on the right. |
| Precedence | Associativity | Category |
|---|---|---|
| ( ) [ ] | left to right | Function Call, Parentheses, Array Index |
| - | right to left | Unary Negation |
| * / % | left to right | Multiplication |
| + - | left to right | Addition |
| >= <= < > | left to right | Relational Operators |
| == != | left to right | Equality Operators |
| ! | right to left | Logical "not" |
| & | left to right | Bitwise "and" |
| ^ | left to right | Exclusive "or" (XOR) |
| | | left to right | Bitwise "or" |
| && | left to right | Logical "and" |
| || | left to right | Logical "or" |
| = | right to left | Assignment |
| , | left to right | Comma |
| Operators are listed in order of descending precedence. | ||
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.
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.