- Introduction
- Editor
- Get started
- Runtime / VM
Language Guide
Design-Notes
Operators
Operators are used to perform operations on variables and values
The following table provides information about operators
| Type | Name | Description |
|---|---|---|
| + | Addition | Adds together two values |
| - | Subtraction | Subtracts one value from another |
| * | Multiplication | Multiplies two values |
| / | Division | Divides one value by another |
| % | Modulus | Returns the division remainder |
| &&, and | Logical and | Returns true if both statements are true |
| ||, or | Logical or | Returns true if one of the statements is true |
| !, not | Logical not | Reverse the result, returns false if the result is true |
| == | Equal | Compare values for equality |
| != | Not equal | Returns true if values are not equal |
| >= | Greater than or equal | Returns true if left value is greater than or equal to right value |
| <= | Less than or equal | Returns true if left value is less than or equal to right value |
| === | Strict equal | Compare pointer/reference equality (same memory address) |
| !== | Strict not equal | Compare raw C++ pointer inequality (different memory address) |
| += | Add and assign | Add right value to variable and assign the result |
| -= | Subtract and assign | Subtract right value from variable and assign the result |
| *= | Multiply and assign | Multiply variable by right value and assign the result |
| /= | Divide and assign | Divide variable by right value and assign the result |
Example
val a = 10
val b = 3
// Arithmetic
println(a + b) // 13
println(a - b) // 7
println(a * b) // 30
println(a / b) // 3
println(a % b) // 1
// Comparison
println(a == b) // false
println(a != b) // true
println(a >= b) // true
println(a <= b) // false
// Logical
val x = true
val y = false
println(x && y) // false
println(x and y) // false
println(x || y) // true
println(x or y) // true
println(!x) // false
// Assignment
var c = 5
c += 2 // 7
c -= 1 // 6
c *= 3 // 18
c /= 2 // 9
println(c)
// Strict / pointer comparison
val obj1 = 1
val obj2 = obj1
val obj3 = 1
println(obj1 === obj2) // true (same pointer)
println(obj1 !== obj3) // true (different pointer)