Operators are special symbols or keywords that perform operations on one or more operands (variables, literals, expressions).
| Operator | Name | Example | Result | Notes |
|---|---|---|---|---|
+ |
Addition | 5 + 3 |
8 | Also string concatenation |
- |
Subtraction | 10 - 4 |
6 | Unary minus: -x |
* |
Multiplication | 6 * 7 |
42 | — |
/ |
Division | 10 / 3 |
3 | Integer division truncates toward zero |
% |
Modulus | 10 % 3 |
1 | Remainder |
Compound assignment (shorthand): += -= *= /= %=
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal | 5 == 5 |
true |
!= |
Not equal | 5 != 3 |
true |
< |
Less than | 4 < 10 |
true |
> |
Greater than | 15 > 7 |
true |
<= |
Less than or equal | 5 <= 5 |
true |
>= |
Greater than or equal | 8 >= 10 |
false |
| Operator | Name | Example | Result when... | Short-circuit? |
|---|---|---|---|---|
&& |
AND | true && false |
true only if both are true |
Yes |
| ` | ` | OR | `true | |
! |
NOT | !true |
Reverses the value | — |
Example combining operators
int a = 17, b = 5;
bool isBig = a > 10 && (a % 2 == 1); // true
bool safeDivide = b != 0 && (a / b > 3); // short-circuit prevents /0Operator Precedence
- ! (not)
- * / % (multiplication, division, modulus)
- + - (addition, subtraction)
- < > <= >= (relational and type testing)
- == != (equality and inequality)
- && (logical AND)
- || (logical OR)