Operators
Arithmetic
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 3 + 4 |
7 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 6 * 7 |
42 |
/ |
Division | 15 / 4 |
3 |
% |
Modulo | 17 % 5 |
2 |
- |
Negation | -x |
negated |
Type behavior
When mixing int and float in arithmetic, the result is always a float:
Integer division truncates toward zero:
Division by zero produces a runtime error.
String concatenation
The + operator concatenates two strings:
Both sides must be strings. To concatenate a number with a string, convert it first:
Comparison
| Operator | Description | Example |
|---|---|---|
== |
Equal | x == y |
!= |
Not equal | x != y |
< |
Less than | x < y |
> |
Greater than | x > y |
<= |
Less or equal | x <= y |
>= |
Greater or equal | x >= y |
Comparison operators return a bool. Numeric comparisons work across int and float:
Equality for strings compares by value:
Logical
| Operator | Description | Example |
|---|---|---|
and |
Logical AND | x > 0 and x < 10 |
or |
Logical OR | x == 0 or x == 1 |
not |
Logical NOT | not done |
and and or use short-circuit evaluation. If the left side of and is false, the right side is not evaluated. If the left side of or is true, the right side is not evaluated.
Operator precedence
From lowest to highest:
| Priority | Operators |
|---|---|
| 1 | or |
| 2 | and |
| 3 | ==, != |
| 4 | <, >, <=, >= |
| 5 | +, - |
| 6 | *, /, % |
| 7 | not, unary - |
| 8 | ., (), [] |
Use parentheses to override precedence: