Lesson 04

Arithmetic

Number operations in Krypton All values are strings, but numeric operations work on numeric strings

← All lessons   Prev   Next →

// Tutorial 04: Arithmetic
// Number operations in Krypton
// All values are strings, but numeric operations work on numeric strings

just run {
    let a = 10
    let b = 3

    kp("a = " + a)
    kp("b = " + b)

    // Basic operations
    kp("a + b = " + (a + b))
    kp("a - b = " + (a - b))
    kp("a * b = " + (a * b))
    kp("a / b = " + (a / b))   // Integer division
    kp("a % b = " + (a % b))   // Modulo (remainder)

    // Order of operations
    let result = 2 + 3 * 4
    kp("2 + 3 * 4 = " + result)

    let result2 = (2 + 3) * 4
    kp("(2 + 3) * 4 = " + result2)
}
Click ▶ Run to execute. Lessons using k:fs/k:http/match/struct need the real runtime.

Tip: this code box is editable — tweak it, then hit Run, or copy into a .k file and run locally.

Run it locally: kcc -r tutorial/04_arithmetic.k

← All lessons   Prev   Next →