Table of Contents

Operations and expressions

Arithmetic

Nyx supports the standard math operations:

fn main() {
    let a: int = 10
    let b: int = 3

    print(a + b)    // 13   addition
    print(a - b)    // 7    subtraction
    print(a * b)    // 30   multiplication
    print(a / b)    // 3    division (integer — no decimals)
    print(a % b)    // 1    remainder (modulo)
}

When dividing two integers, the result is an integer — the decimal part is dropped, not rounded. 10 / 3 gives 3, not 3.33.

For decimal division, use floats:

fn main() {
    let a: float = 10.0
    let b: float = 3.0
    print(a / b)    // 3.333...
}

Compound assignment

When you want to update a variable using its current value, you can use shorthand operators:

fn main() {
    var x: int = 10

    x += 5     // same as: x = x + 5    → 15
    x -= 3     // same as: x = x - 3    → 12
    x *= 2     // same as: x = x * 2    → 24
    x /= 4     // same as: x = x / 4    → 6
    x %= 5     // same as: x = x % 5    → 1

    print(x)   // 1
}

Comparison operators

Comparisons produce a bool value — either true or false:

fn main() {
    let a: int = 10
    let b: int = 20

    print(a == b)    // false   equal to
    print(a != b)    // true    not equal to
    print(a < b)     // true    less than
    print(a > b)     // false   greater than
    print(a <= b)    // true    less than or equal
    print(a >= b)    // false   greater than or equal
}

You can compare strings too:

fn main() {
    let name: String = "Alice"
    print(name == "Alice")    // true
    print(name == "Bob")      // false
    print(name != "Bob")      // true
}

Logical operators

Logical operators combine boolean values:

fn main() {
    let sunny: bool = true
    let warm: bool = false

    print(sunny and warm)     // false  (both must be true)
    print(sunny or warm)      // true   (at least one is true)
    print(not sunny)          // false  (inverts the value)
}

Truth table:

A       B       A and B     A or B      not A
true    true    true        true        false
true    false   false       true        false
false   true    false       true        true
false   false   false       false       true

String concatenation

The + operator joins strings together:

fn main() {
    let first: String = "Hello"
    let second: String = "World"
    let result: String = first + ", " + second + "!"

    print(result)    // Hello, World!
}

Remember: you cannot add a string and a number directly. Convert the number first:

fn main() {
    let name: String = "Alice"
    let age: int = 25
    print(name + " is " + int_to_string(age) + " years old")
    // Alice is 25 years old
}

Order of operations

Nyx follows standard math precedence:

  1. Parentheses () — evaluated first
  2. Multiplication *, division /, modulo %
  3. Addition +, subtraction -
  4. Comparisons ==, !=, <, >, <=, >=
  5. Logical not, then and, then or
fn main() {
    print(2 + 3 * 4)       // 14, not 20 (multiplication first)
    print((2 + 3) * 4)     // 20 (parentheses first)
    print(10 - 2 * 3 + 1)  // 5
}

When in doubt, use parentheses. They make your intent clear.

Exercises

  1. Write a program that calculates the area of a rectangle with width 7 and height 12.
  1. A store has an item priced at 29.99 with a 15% discount. Calculate the final price.
  1. Write a program that converts a temperature from Celsius to Fahrenheit. The formula is: F = C * 9 / 5 + 32. Test with 0°C, 100°C, and 37°C.
  1. Given the values a = 5, b = 10, c = 3, what is the result of a + b c? What about (a + b) c? Verify with Nyx.

Summary

Next chapter: Control flow →

← Previous: Variables and types Next: Control flow →