Control Flow

Master program flow in Nebula.

Conditionals

If Statement

perm age = 18

if age >= 18 do
    log("Adult")
end

If-Else

perm score = 75

if score >= 60 do
    log("Passed")
else
    log("Failed")
end

If-Elsif-Else

perm grade = 85

if grade >= 90 do
    log("A - Excellent!")
elsif grade >= 80 do
    log("B - Good job!")
elsif grade >= 70 do
    log("C - Satisfactory")
elsif grade >= 60 do
    log("D - Needs improvement")
else
    log("F - Failed")
end

Nested Conditions

perm user_type = "premium"
perm logged_in = on

if logged_in do
    if user_type == "premium" do
        log("Full access granted")
    elsif user_type == "basic" do
        log("Limited access")
    else
        log("Guest access")
    end
else
    log("Please log in")
end

Complex Conditions

perm age = 25
perm has_license = on
perm has_insurance = on

if age >= 18 and has_license and has_insurance do
    log("You can rent a car")
elsif age >= 18 and has_license do
    log("Need insurance")
elsif age >= 18 do
    log("Need license and insurance")
else
    log("Too young")
end

Loops

While Loop

# Basic while
i = 0
while i < 5 do
    log(i)
    i = i + 1
end
# Output: 0 1 2 3 4

# Infinite loop with break
while on do
    perm input = get()
    if input == "quit" do
        break
    end
    log("You said:", input)
end

For Loop

Range-based iteration:

# Basic range (inclusive)
for i = 1, 5 do
    log(i)
end
# Output: 1 2 3 4 5

# With step
for i = 0, 10, 2 do
    log(i)
end
# Output: 0 2 4 6 8 10

# Countdown
for i = 5, 1, -1 do
    log(i)
end
log("Liftoff!")
# Output: 5 4 3 2 1 Liftoff!

Each Loop

Iterate over collections:

# Lists
perm fruits = ["apple", "banana", "cherry"]
each fruit in fruits do
    log("I like", fruit)
end

# With index tracking
perm items = ["a", "b", "c"]
i = 0
each item in items do
    log(i, "->", item)
    i = i + 1
end

# Maps
perm data = {"x": 10, "y": 20}
each key in data do
    log(key, "=", data[key])
end

Loop Control

Break

Exit loop immediately:

# Find first match
perm numbers = [1, 3, 5, 8, 9, 10]

each n in numbers do
    if n % 2 == 0 do
        log("First even:", n)
        break
    end
end
# Output: First even: 8

# Exit infinite loop
count = 0
while on do
    count = count + 1
    if count >= 5 do
        break
    end
    log(count)
end

Continue

Skip to next iteration:

# Skip even numbers
for i = 0, 10 do
    if i % 2 == 0 do
        continue
    end
    log(i)
end
# Output: 1 3 5 7 9

# Skip empty values
perm items = ["a", empty, "b", empty, "c"]
each item in items do
    if item == empty do
        continue
    end
    log(item)
end
# Output: a b c

Nested Loop Control

# Break only affects innermost loop
for i = 0, 2 do
    for j = 0, 2 do
        if j == 1 do
            break  # Only breaks inner loop
        end
        log(i, j)
    end
end
# Output: (0,0) (1,0) (2,0)

Pattern Matching

Basic Match

perm status = 404

match status do
    200 => log("OK"),
    201 => log("Created"),
    400 => log("Bad Request"),
    404 => log("Not Found"),
    500 => log("Server Error"),
    _ => log("Unknown status"),
end

Match with Actions

fn get_day_name(day) do
    match day do
        1 => give "Monday",
        2 => give "Tuesday",
        3 => give "Wednesday",
        4 => give "Thursday",
        5 => give "Friday",
        6 => give "Saturday",
        7 => give "Sunday",
        _ => give "Invalid day",
    end
end

log(get_day_name(3))  # Wednesday

Match Booleans

perm success = on

match success do
    on => log("Operation succeeded!"),
    off => log("Operation failed"),
end

Match Strings

perm command = "start"

match command do
    "start" => log("Starting server..."),
    "stop" => log("Stopping server..."),
    "restart" => do
        log("Stopping...")
        log("Starting...")
    end,
    "status" => log("Server is running"),
    _ => log("Unknown command:", command),
end

Ternary Operator

Inline conditional:

perm age = 20
perm status = age >= 18 ? "adult" : "minor"
log(status)  # adult

# Nested ternary
perm score = 85
perm grade = score >= 90 ? "A" : score >= 80 ? "B" : "C"
log(grade)  # B

# In expressions
log("Status:", age >= 18 ? "OK" : "Too young")

Guard Clauses

Early returns simplify logic:

fn process_user(user) do
    # Guard clauses
    if user == empty do
        log("No user provided")
        give empty
    end
    
    if user["active"] == off do
        log("User is inactive")
        give empty
    end
    
    if user["age"] < 18 do
        log("User too young")
        give empty
    end
    
    # Main logic (only reached if all guards pass)
    log("Processing user:", user["name"])
    give on
end

State Machines

perm state = "idle"

while on do
    match state do
        "idle" => do
            log("Waiting for input...")
            state = "processing"
        end,
        "processing" => do
            log("Processing data...")
            state = "complete"
        end,
        "complete" => do
            log("Done!")
            break
        end,
    end
end