Skip to main content

Control Flow

The Alia language provides a variety of control flow structures for controlling the execution path of code.

1. Conditional Statements​

if Statement​

alias if <Boolean condition> <Stack thenStack>
alias if <Boolean condition> <Stack thenStack> else <Stack elseStack>

Example​

const age = 18
if age >= 18 {
print 'Adult'
} else {
print 'Minor'
}
const score = 85
if score >= 90 {
print 'Excellent'
} else {
if score >= 80 {
print 'Good'
} else {
if score >= 60 {
print 'Pass'
} else {
print 'Fail'
}
}
}

switch Statement​

switch matches one value against multiple branches. The case/multicase clauses are matched in declaration order; the first matching clause executes its body and subsequent clauses are skipped; default executes when no clause matches. The value of the switch expression is the return value of the first matching clause; if no clause matches and there is no default, it is the value of the last statement in the switch body.

alias switch <Value switchVal> <Stack switchBody>
alias case <Value caseVal> <Stack caseBody>
alias multicase <Array caseVals> <Stack caseBody>
alias default <Stack defaultBody>

Basic case​

const fruit = 'apple'
switch fruit {
case 'apple' {
print 'Apple'
}
case 'banana' {
print 'Banana'
}
}

switch as an Expression​

switch returns the value of the first matching clause and can be used directly as an expression:

const score = 2
const level = switch score {
case 1 { 'low' }
case 2 { 'medium' }
case 3 { 'high' }
default { 'unknown' }
}
print level

multicase Compound Conditions​

multicase accepts an array of values; any equal value hits, and it is used to group multiple values into the same branch:

const day = 6
switch day {
multicase [1, 2, 3, 4, 5] {
print 'Weekday'
}
multicase [6, 7] {
print 'Weekend'
}
}

The default Branch​

default executes when none of the case/multicase clauses match:

const code = 404
switch code {
case 200 {
print 'OK'
}
case 404 {
print 'Not Found'
}
default {
print 'Unknown status code'
}
}

First Match Wins​

Once a clause matches, all subsequent clauses are skipped:

let count = 0
switch 2 {
case 2 { count = count + 1 }
case 2 { count = count + 100 }
default { count = count + 1000 }
}
print count

Type Distinction​

case matches by value and distinguishes types: case 42 does not match the string '42'.

const hit = switch '42' {
case 42 { 'number' }
case '42' { 'string' }
default { 'other' }
}
print hit

2. Loop Statements​

for Loop​

alias for <Stack init> <Boolean condition> <Stack update> <Stack body>

Example​

for { let i = 0 } i < 10 { i ++ } {
print i
}
const arr = [1, 2, 3, 4, 5]
for { let i = 0 } i < arr.length { i ++ } {
print 'Element ' .. i .. ': ' .. arr[i]
}

while Loop​

alias while <Boolean condition> <Stack body>

Example​

let i = 1
while i <= 5 {
print i
i ++
}
let count = 0
while count < 3 {
print 'Iteration count: ' .. count
count ++
}

each Loop​

Used to iterate over every element of an array.

alias each <Variable arrayItem> in <Array target> <Stack body>
alias each <Variable arrayItem> with <Variable updateCount> in <Array target> <Stack body>

Basic Syntax​

The basic form iterates over every element of the array and binds each element to the given variable.

each with a Counter​

The counter form maintains a count value while iterating; the count starts at 1.

Example​

const fruits = ['Apple', 'Banana', 'Orange']
each fruit in fruits {
print fruit
}
const nums = [10, 20, 30]
let total = 0
each num in nums {
total += num
}
print total
const data = [1, 2, 3, 4, 5]
each item in data {
if item > 3 {
break
}
print item
}

repeat Loop​

alias repeat <Stack body>

Example​

let i = 0
try {
repeat {
print i
i ++
if i >= 5 {
throw 'done'
}
}
} catch e {
print e
}

3. Jump Statements​

break Statement​

Used to exit a loop or stack.

alias break
alias break <Value returnVal>

Example​

for { let i = 0 } i < 10 { i ++ } {
if i == 5 {
break
}
print i
}
const stack = {
print 'start'
break 'Found 5'
print 'end'
}
const result = call stack.run
print result

return Statement​

Used to return a value from a function or stack.

alias return
alias return <Value returnVal>

Example​

const add = alias <Number a> <Number b> -> {
return a + b
}
const sum = call add [1, 2]
print sum

The Has Keyword​

Used to check whether a specified variable exists inside a mutable stack; the check result is of Boolean type.

alias Has <Variable name>
alias Has <String name>

Example​

const stack = mut *{
if Has 'count' {
count
} else {
let count = 1
count
}
}
const first = call stack.run
print first
const second = call stack.run
print second

The enter Keyword​

Used to enter a stack's context, similar to JavaScript's with but safer.

alias enter <Stack stack>
alias enter <Value stackVal> <Stack body>

Example​

const stack = mut *{
let val = 100
val
}
call stack.run
enter (stack) {
print val
}

4. Error Handling​

try-catch Statement​

alias try <Stack tryStack> catch <Variable e> <Stack catchStack>

Example​

try {
print 'Attempting execution'
throw 'An error occurred'
} catch e {
print 'Caught error: ' .. e
}

5. Example Code​

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let sum = 0
let evenCount = 0
let oddCount = 0
for { let i = 0 } i < numbers.length { i ++ } {
const num = numbers[i]
if num % 2 == 0 {
evenCount ++
print num .. ' is even'
} else {
oddCount ++
print num .. ' is odd'
}
sum += num
if sum > 20 {
print 'Sum exceeds 20, breaking out of the loop'
break
}
}
print 'Even count: ' .. evenCount
print 'Odd count: ' .. oddCount
print 'Total sum: ' .. sum

Next Steps​