Control Flow
Zeus provides standard control flow constructs for conditional execution and iteration.
If Statements
Basic If
Execute code conditionally:
function main(): i32 { let age: i32 = 20;
if (age >= 18) { return 1; // Adult }
return 0; // Minor}If-Else
Handle both branches:
function main(): i32 { let score: i32 = 75;
if (score >= 60) { return 1; // Pass } else { return 0; // Fail }}Nested If-Else
Chain multiple conditions:
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 } } } }}
function main(): i32 { return getGrade(85); // Returns 3 (B)}Conditions
Conditions must evaluate to boolean:
let x: i32 = 10;let y: i32 = 20;
// Comparison operatorsif (x == y) { } // Equalif (x != y) { } // Not equalif (x < y) { } // Less thanif (x <= y) { } // Less than or equalif (x > y) { } // Greater thanif (x >= y) { } // Greater than or equal
// Boolean valueslet flag: boolean = true;if (flag) { }
// Negationif (!flag) { }While Loops
Repeat while a condition is true:
function main(): i32 { let i: i32 = 0; let sum: i32 = 0;
while (i < 10) { sum = sum + i; i = i + 1; }
return sum; // Returns 45 (0+1+2+...+9)}Counting Loop
function factorial(n: i32): i32 { let result: i32 = 1; let i: i32 = 1;
while (i <= n) { result = result * i; i = i + 1; }
return result;}
function main(): i32 { return factorial(5); // Returns 120}Conditional Loop
function findFirstPositive(arr: i32[], size: i32): i32 { let i: i32 = 0;
while (i < size) { if (arr.get(i) > 0) { return arr.get(i); } i = i + 1; }
return 0; // Not found}Control Flow Patterns
Early Return
Exit a function early when a condition is met:
function isEven(n: i32): boolean { if (n == 0) { return true; }
// More complex check could go here let remainder: i32 = n - (n / 2) * 2; return remainder == 0;}Accumulator Pattern
Build up a result through iteration:
function sumArray(arr: i32[], length: i32): i32 { let sum: i32 = 0; let i: i32 = 0;
while (i < length) { sum = sum + arr.get(i); i = i + 1; }
return sum;}Search Pattern
Find an element in a collection:
function contains(arr: i32[], length: i32, target: i32): boolean { let i: i32 = 0;
while (i < length) { if (arr.get(i) == target) { return true; } i = i + 1; }
return false;}Counter Pattern
Count occurrences:
function countPositive(arr: i32[], length: i32): i32 { let count: i32 = 0; let i: i32 = 0;
while (i < length) { if (arr.get(i) > 0) { count = count + 1; } i = i + 1; }
return count;}Nested Loops
Loops can be nested for multi-dimensional iteration:
function multiplicationTable(): i32 { let sum: i32 = 0; let i: i32 = 1;
while (i <= 5) { let j: i32 = 1;
while (j <= 5) { sum = sum + (i * j); j = j + 1; }
i = i + 1; }
return sum;}Practical Examples
Fibonacci
function fibonacci(n: i32): i32 { if (n <= 1) { return n; }
let prev: i32 = 0; let curr: i32 = 1; let i: i32 = 2;
while (i <= n) { let next: i32 = prev + curr; prev = curr; curr = next; i = i + 1; }
return curr;}
function main(): i32 { return fibonacci(10); // Returns 55}Maximum Value
function findMax(arr: i32[], length: i32): i32 { if (length == 0) { return 0; }
let max: i32 = arr.get(0); let i: i32 = 1;
while (i < length) { if (arr.get(i) > max) { max = arr.get(i); } i = i + 1; }
return max;}