Skip to content

Control Flow

Zeus provides standard control flow constructs for conditional execution and iteration.

If Statements

Basic If

function main(): i32 {
let age = 20;
if (age >= 18) {
return 1; // adult
}
return 0; // minor
}

If-Else

function main(): i32 {
let score = 75;
if (score >= 60) {
return 1; // pass
} else {
return 0; // fail
}
}

Chained If-Else

function getGrade(score: i32): i32 {
if (score >= 90) {
return 4; // A
} else {
if (score >= 80) {
return 3; // B
} else {
if (score >= 70) {
return 2; // C
} else {
if (score >= 60) {
return 1; // D
} else {
return 0; // F
}
}
}
}
}

Conditions

Conditions must evaluate to boolean:

let x: i32 = 10;
let y: i32 = 20;
if (x == y) { } // equal
if (x != y) { } // not equal
if (x < y) { } // less than
if (x <= y) { } // less than or equal
if (x > y) { } // greater than
if (x >= y) { } // greater than or equal
let flag: boolean = true;
if (flag) { } // boolean variable
if (!flag) { } // negation
if (x > 0 && y > 0) { } // logical AND
if (x > 0 || y > 0) { } // logical OR

For Loops

The for loop is ideal for counted iteration:

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

Syntax

for (initializer; condition; update) {
// body
}
  • Initializer — runs once before the loop starts (usually declares the loop variable)
  • Condition — checked before each iteration; the loop runs while true
  • Update — runs after each iteration (usually advances the counter)

All three parts are optional. Omitting the condition creates an infinite loop.

Examples

Counting down:

for (let i: i32 = 10; i > 0; i--) {
// i = 10, 9, 8, …, 1
}

Stepping by 2:

for (let i: i32 = 0; i < 10; i += 2) {
// i = 0, 2, 4, 6, 8
}

Nested loops:

function main(): i32 {
let sum = 0;
for (let i: i32 = 0; i < 3; i++) {
for (let j: i32 = 0; j < 3; j++) {
sum += i * j;
}
}
return sum;
}

While Loops

Repeat while a condition holds:

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

Factorial

function factorial(n: i32): i32 {
let result = 1;
let i = 1;
while (i <= n) {
result *= i;
i += 1;
}
return result;
}

Break

break exits the innermost loop immediately. Code after break in the same iteration is skipped, and execution continues after the loop.

function main(): i32 {
let found = -1;
for (let i: i32 = 0; i < 100; i++) {
if (i * i > 50) {
found = i;
break; // stop searching once the condition is met
}
}
return found; // 8 (8*8 = 64 > 50)
}

Break in a While Loop

function findFactor(n: i32): i32 {
let i = 2;
while (i < n) {
if (n % i == 0) {
break; // found a factor
}
i += 1;
}
return i; // smallest factor, or n if prime
}

Break in Nested Loops

break only exits the innermost loop. The outer loop continues normally:

function main(): i32 {
let total = 0;
for (let i: i32 = 0; i < 5; i++) {
for (let j: i32 = 0; j < 10; j++) {
if (j == 3) {
break; // exits only the inner loop
}
total += 1;
}
// outer loop continues here
}
return total; // 15 (5 outer iters × 3 inner iters each)
}

Continue

continue skips the rest of the current iteration and moves to the next one. In a for loop the update expression (i++) still runs before the next condition check.

function main(): i32 {
let sum = 0;
// Sum only odd numbers 1..9
for (let i: i32 = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // skip even numbers
}
sum += i;
}
return sum; // 25 (1+3+5+7+9)
}

Continue in a While Loop

function sumOdd(limit: i32): i32 {
let sum = 0;
let i = 0;
while (i < limit) {
i += 1; // advance first — otherwise infinite loop
if (i % 2 == 0) {
continue; // skip even numbers
}
sum += i;
}
return sum;
}

Continue in Nested Loops

Like break, continue only affects the innermost loop:

function main(): i32 {
let count = 0;
for (let i: i32 = 0; i < 3; i++) {
for (let j: i32 = 0; j < 3; j++) {
if (j == 1) {
continue; // skips j == 1 in inner loop only
}
count += 1;
}
}
return count; // 6 (3 outer × 2 inner where j ≠ 1)
}

Patterns

Search with Break

The most idiomatic way to exit a loop once a result is found:

function indexOf(arr: i32[], size: i32, target: i32): i32 {
let idx = -1;
for (let i: i32 = 0; i < size; i++) {
if (arr.get(i) == target) {
idx = i;
break;
}
}
return idx;
}

Filter with Continue

Skip unwanted elements and only process the ones that match:

function sumPositive(arr: i32[], size: i32): i32 {
let sum = 0;
for (let i: i32 = 0; i < size; i++) {
if (arr.get(i) <= 0) {
continue;
}
sum += arr.get(i);
}
return sum;
}

Accumulator

function sumArray(arr: i32[], length: i32): i32 {
let sum = 0;
let i = 0;
while (i < length) {
sum += arr.get(i);
i += 1;
}
return sum;
}

Maximum Value

function findMax(arr: i32[], length: i32): i32 {
if (length == 0) { return 0; }
let max = arr.get(0);
let i = 1;
while (i < length) {
if (arr.get(i) > max) {
max = arr.get(i);
}
i += 1;
}
return max;
}

Fibonacci (iterative)

function fibonacci(n: i32): i32 {
if (n <= 1) { return n; }
let prev = 0;
let curr = 1;
for (let i: i32 = 2; i <= n; i++) {
let next = prev + curr;
prev = curr;
curr = next;
}
return curr;
}