Skip to content

Operators

Zeus provides a familiar set of operators for arithmetic, comparison, and logical operations.

Arithmetic Operators

OperatorDescriptionExample
+Addition (numbers) or concatenation (strings)5 + 38, "a" + "b""ab"
-Subtraction5 - 32
*Multiplication5 * 315
/Division15 / 35
%Modulo (remainder)7 % 31
**Power (exponentiation)2.0 ** 3.08.0

Examples

function main(): i32 {
let a: i32 = 10;
let b: i32 = 3;
let sum: i32 = a + b; // 13
let diff: i32 = a - b; // 7
let product: i32 = a * b; // 30
let quotient: i32 = a / b; // 3 (integer division)
return sum + diff + product + quotient;
}

Integer Division

Division between integers produces an integer result:

let result: i32 = 7 / 2; // 3, not 3.5

For floating-point division, use float types:

let result: f64 = 7.0 / 2.0; // 3.5

Modulo Operator

The modulo operator returns the remainder of division:

let remainder: i32 = 17 % 5; // 2
let isEven: boolean = (10 % 2) == 0; // true

Power Operator

The power operator performs exponentiation. Note that it currently works with floating-point numbers:

let squared: f64 = 2.0 ** 2.0; // 4.0
let cubed: f64 = 2.0 ** 3.0; // 8.0

Unary Minus

Negate a value:

let positive: i32 = 42;
let negative: i32 = -positive; // -42

Comparison Operators

OperatorDescriptionExample
==Equal to5 == 5true
!=Not equal to5 != 3true
<Less than3 < 5true
<=Less than or equal5 <= 5true
>Greater than5 > 3true
>=Greater than or equal5 >= 5true

Examples

function compare(a: i32, b: i32): i32 {
if (a == b) {
return 0; // Equal
}
if (a < b) {
return -1; // a is smaller
}
return 1; // a is larger
}
function main(): i32 {
return compare(10, 5); // Returns 1
}

Comparing Different Types

Comparison works between compatible types:

let intVal: i32 = 5;
let floatVal: f64 = 5.0;
// This works because i32 can be implicitly converted to f64
let equal: boolean = floatVal == intVal; // true

String Operators

Zeus supports operators for string manipulation:

OperatorDescriptionExample
+Concatenation"Hello" + " World""Hello World"
==Equal to"abc" == "abc"true
!=Not equal to"abc" != "xyz"true
<Lexicographically less"apple" < "banana"true
<=Less than or equal"abc" <= "abc"true
>Lexicographically greater"banana" > "apple"true
>=Greater than or equal"abc" >= "abc"true

String Concatenation

let greeting: string = "Hello" + " " + "World";
log(greeting); // "Hello World"

String Comparison

let a: string = "apple";
let b: string = "banana";
let c: string = "apple";
if (a == c) {
log("a equals c"); // This executes
}
if (a < b) {
log("apple comes before banana"); // This executes
}

Logical Operators

OperatorDescriptionExample
!Logical NOT!truefalse
&&Logical ANDtrue && falsefalse
||Logical ORtrue || falsetrue

Examples

function main(): i32 {
let a: boolean = true;
let b: boolean = false;
let notA: boolean = !a; // false
let andResult: boolean = a && b; // false
let orResult: boolean = a || b; // true
// Short-circuit evaluation
if (a || expensiveCheck()) {
// expensiveCheck() may not be called if a is true
}
return 0;
}

Combining Conditions

function isInRange(x: i32, min: i32, max: i32): boolean {
return x >= min && x <= max;
}
function isValidInput(value: i32): boolean {
return value > 0 || value == -1; // -1 is a special "any" value
}

Assignment Operators

Basic Assignment

The = operator assigns a value to a variable:

let x: i32 = 10; // Initial assignment
x = 20; // Reassignment
x = x + 5; // Update based on current value

Compound Assignment Operators

Zeus supports compound assignment operators that combine arithmetic and assignment:

OperatorDescriptionEquivalent
+=Add and assignx = x + y
-=Subtract and assignx = x - y
*=Multiply and assignx = x * y
/=Divide and assignx = x / y
%=Modulo and assignx = x % y
function main(): i32 {
let x: i32 = 10;
x += 5; // x is now 15
x -= 3; // x is now 12
x *= 2; // x is now 24
x /= 4; // x is now 6
x %= 4; // x is now 2
return x;
}

Increment and Decrement Operators

Zeus supports both prefix and postfix increment/decrement operators:

OperatorDescriptionExample
++xPrefix increment (increment, then return new value)let y: i32 = ++x;
x++Postfix increment (return old value, then increment)let y: i32 = x++;
--xPrefix decrement (decrement, then return new value)let y: i32 = --x;
x--Postfix decrement (return old value, then decrement)let y: i32 = x--;

Prefix vs Postfix

function main(): i32 {
let a: i32 = 5;
let b: i32 = ++a; // a is 6, b is 6 (increment first)
let c: i32 = 5;
let d: i32 = c++; // c is 6, d is 5 (return old value first)
return 0;
}

Common Usage in Loops

function main(): i32 {
let sum: i32 = 0;
for (let i: i32 = 0; i < 10; i++) {
sum += i;
}
return sum; // 45
}

Operator Precedence

Operators follow standard mathematical precedence:

PrecedenceOperatorsDescription
Highest++, -- (postfix)Postfix increment/decrement
., []Member access, indexing
()Function call
++, -- (prefix), !, -Prefix increment/decrement, NOT, negation
**Power (right-associative)
*, /, %Multiplication, division, modulo
+, -Addition, subtraction
<, <=, >, >=Comparison
==, !=Equality
&&Logical AND
||Logical OR
Lowest=, +=, -=, *=, /=, %=Assignment

Examples

let result: i32 = 2 + 3 * 4; // 14 (not 20)
let result2: i32 = (2 + 3) * 4; // 20 (parentheses first)
let result3: i32 = 10 - 5 - 2; // 3 (left to right)

Practical Examples

Absolute Value

function abs(n: i32): i32 {
if (n < 0) {
return -n;
}
return n;
}

Distance Formula (simplified)

function manhattanDistance(x1: i32, y1: i32, x2: i32, y2: i32): i32 {
let dx: i32 = x2 - x1;
let dy: i32 = y2 - y1;
if (dx < 0) {
dx = -dx;
}
if (dy < 0) {
dy = -dy;
}
return dx + dy;
}

Min and Max

function min(a: i32, b: i32): i32 {
if (a < b) {
return a;
}
return b;
}
function max(a: i32, b: i32): i32 {
if (a > b) {
return a;
}
return b;
}
function clamp(value: i32, minVal: i32, maxVal: i32): i32 {
return max(minVal, min(value, maxVal));
}

Next

Modules →