AutolangDocs

Operators

Operators are used to perform operations on variables and values.

Operator Reference

OperatorNameDescription
+AdditionAdds together two values
-SubtractionSubtracts one value from another
*MultiplicationMultiplies two values
/DivisionDivides one value by another
%ModulusReturns the division remainder
&&Logical ANDReturns true if both statements are true
||Logical ORReturns true if one of the statements is true
!Logical NOTReverses the result — returns false if the result is true
inMembership / Range checkReturns true if the left value is in a range (x in 1..9), an Array, a Set, or a Map key
??Null coalescingReturns the left side if non-null, otherwise the right side. E.g. a?.get() ?? 100
==EqualCompare values for equality
!=Not equalReturns true if values are not equal
>=Greater than or equalReturns true if left value is ≥ right value
<=Less than or equalReturns true if left value is ≤ right value
===Strict equalCompares pointer/reference equality (same memory address)
!==Strict not equalReturns true if the two objects are at different memory addresses
+=Add and assignAdd right value to variable and assign the result
-=Subtract and assignSubtract right value from variable and assign the result
*=Multiply and assignMultiply variable by right value and assign the result
/=Divide and assignDivide 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 || y) // true println(!x) // false // Membership / Range check val list = <Int>[1, 2, 3, 4, 5] println(3 in list) // true println(3 in 1..9) // true (inclusive range) println(3 in 0..<3) // false (exclusive upper) // Null coalescing (??) var s: String? = null println(s ?? "default") // default // 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)