Operators

Arithmetic Operators

kode
let a = 10
let b = 3

let sum = a + b         // 13
let diff = a - b        // 7
let product = a * b     // 30
let quotient = a / b    // 3 (integer division)
let remainder = a % b   // 1

Compound Assignment

kode
let x = 10
x += 5    // x = 15
x -= 3    // x = 12
x *= 2    // x = 24
x /= 4    // x = 6
x %= 4    // x = 2

Increment / Decrement

kode
let n = 5
n++    // n = 6
n--    // n = 5

Comparison Operators

kode
let x = 5
let y = 10

x == y    // false (equal to)
x != y    // true (not equal to)
x > y     // false (greater than)
x < y     // true (less than)
x >= y    // false (greater than or equal)
x <= y    // true (less than or equal)

Logical Operators

kode
let a = true
let b = false

a && b    // false (AND)
a || b    // true (OR)
!a        // false (NOT)

// Integer truthy/falsy (0 is false, non-zero is true)
print(1 && 0)    // false
print(1 || 0)    // true
print(!0)        // true

Bitwise Operators

kode
let x = 12    // Binary: 1100
let y = 5     // Binary: 0101

x & y          // 4 (AND: 0100)
x | y          // 13 (OR: 1101)
x ^ y          // 9 (XOR: 1001)
x << 2         // 48 (Left shift by 2)
x >> 1         // 6 (Right shift by 1)
~y             // -6 (Bitwise NOT)

String Operators

kode
let first = "John"
let last = "Doe"
let full = first + " " + last     // "John Doe" (concatenation)
let repeat = "abc" * 3            // "abcabcabc" (repetition)

// String interpolation
let age = 30
let message = "I am ${age} years old"  // "I am 30 years old"

Operator Precedence

From highest to lowest:

  1. ~ ! (unary)
  2. * / %
  3. + -
  4. << >>
  5. &
  6. ^
  7. |
  8. == != < > <= >=
  9. &&
  10. ||
  11. = (assignment)