Control Flow

Aegis provides standard control structures to direct the flow of your program.

If / Else

Standard conditional logic. The condition must evaluate to a boolean or a truthy value.

var hp = 50

if (hp > 75) {
    print "Healthy"
} else if (hp > 25) {
    print "Injured"
} else {
    print "Critical condition!"
}

Loops

Aegis provides two types of loops: while for indefinite iteration and foreach for iterating over sequences.

While Loop

Repeats a block of code as long as the condition is true.

var i = 3
while (i > 0) {
    print "Countdown: " + i
    i = i - 1
}
print "Liftoff!"

The Foreach Loop

The foreach loop is the primary tool for iteration in Aegis. It works with Ranges, Lists, and Strings.

Syntax

foreach (variable in iterable) {
    // code block
}

Iterating over Lists

var users = ["Alice", "Bob", "Charlie"]

foreach (user in users) {
    print "Hello, " + user + "!"
}

Iterating over Ranges

To repeat an action a specific number of times, use a Range with ...

foreach (i in 1..10) {
    print "Iteration " + i
}

This is generally preferred over the C-style for loop (for (i, 0, 10, 1)) because it is cleaner and easier to read.

Iterating over Strings

You can also iterate over a string character by character.

foreach (char in "Aegis") {
    print char
}

Nesting

Foreach loops can be nested. The loop variable is local to its specific block, preventing conflicts.

var matrix = [ [1, 2], [3, 4] ]

foreach (row in matrix) {
    foreach (cell in row) {
        print cell
    }
}

Loop Control: Break and Continue

You can finely control the execution of loops using break and continue.

Break

Stops the loop immediately and resumes execution after the loop block.

// Stop searching when we find the target
var target = 5
foreach (i in 0..10) {
    if (i == target) {
        print "Found it!"
        break
    }
}

Continue

Skips the rest of the current iteration and jumps directly to the next one (checking the condition in while, or incrementing in for).

// Print only odd numbers
foreach (i in 0..10) {
    // If even, skip printing
    if (i % 2 == 0) { 
        continue 
    }
    print i
}
// Output: 1, 3, 5, 7, 9

Switch

The switch statement simplifies long if/else chains. Aegis switches perform an implicit break (no fall-through).

var status = 200

switch (status) {
    case 200:
        print "OK"
    case 404:
        print "Not Found"
    case 500:
        print "Server Error"
    default:
        print "Unknown status"
}

Ternary Operator

For simple conditions where you want to assign a value based on a check, the standard if/else can be verbose. Aegis provides the Ternary Operator ? : for this purpose.

Syntax

condition ? value_if_true : value_if_false

Example

Instead of:

var status = null
if (age >= 18) {
    status = "Adult"
} else {
    status = "Minor"
}

You can write:

var status = (age >= 18) ? "Adult" : "Minor"

Nesting

Ternary operators can be nested, although this can reduce readability.

var category = (score > 90) ? "A" : ((score > 50) ? "B" : "C")

Null Coalescing Operator (??)

The null coalescing operator ?? is a logical operator that returns its right-hand side operand when its left-hand side operand is null, and returns its left-hand side operand otherwise.

It is cleaner and safer than using || because it only checks for null, not false or 0.

Syntax

left_expr ?? default_value

Examples

Basic Usage:

var user_input = null
var username = user_input ?? "Guest"
print username // "Guest"

Difference with ||:

var count = 0

// || considers 0 as false
print count || 100 // 100 (Unwanted behavior?)

// ?? only cares about null
print count ?? 100 // 0 (Correct!)

Chaining:

var config = null
var env = null
var port = config ?? env ?? 8080
print port // 8080