Skip to main content

Control Flow

Control flow statements are used to control the flow of execution in a program.

if

The if statement is used to execute a block of code if a condition is truthy.

let x = 5
if x > 3 {
Transmission { "x is greater than 3" }
}
// Outputs "x is greater than 3"

An else clause can be added to execute a block of code if it fails to match all other conditions.

let x = 5
if x > 3 {
Transmission { "x is greater than 3" }
} else {
Transmission { "x is less than or equal to 3" }
}
// Outputs "x is greater than 3"

else if blocks can be added to test multiple conditions.

let x = 7
if x > 10 {
Transmission { "x is greater than 10" }
} else if x > 5 {
Transmission { "x is greater than 5" }
} else {
Transmission { "x is less than or equal to 5" }
}
// Outputs "x is greater than 5"

repeat

The repeat loop is used to execute a block of code a fixed number of times.

repeat 5 {
Transmission { "Hello, world!" }
}
// Outputs "Hello, world!" 5 times

while

The while loop is used to execute a block of code while a condition is truthy.

let x = 0
while x < 5 {
Transmission { %(x) }
x += 1
}
// Outputs 0, 1, 2, 3, 4

for

The for loop is used to execute a block of code for each element in a collection.

for x in Range(0, 5) {
Transmission { %(x) }
}
// Outputs 0, 1, 2, 3, 4

The for loop variable can be declared as a context variable by prefixing it with use:

pub fn Example() {
for use x in Range(0, 5) {
TakeXFromContext
}
// Outputs 0, 1, 2, 3, 4
}
pub fn TakeXFromContext([x]) {
Transmission { %(x) }
}

You can provide two loop variables to iterate over the index and value of an Array:

let list = ["Potato", "Tomato", "Banana"]
for index, value in list {
Transmission { %(index + ": " + value) }
}
// Outputs "0: Potato", "1: Tomato", "2: Banana"

You can also iterate over the keys and values of a map using two loop variables:

let map = { potato = 123, banana = 456 }
for key, value in map {
Transmission { %(key + ": " + value) }
}
// Outputs "$potato: 123", "$banana: 456"

loop

The loop statement is used to execute a block of code indefinitely.

let x = 0
loop {
Transmission { %(x) }
x += 1
if x >= 5 {
break
}
}

break

The break statement is used to exit a loop early. It can be used with repeat, while, and for loops, as well as on and with blocks.

let x = 0
while true {
Transmission { %(x) }
x += 1
if x == 5 {
break
}
}
// Outputs 0, 1, 2, 3, 4

continue

The continue statement is used to skip the rest of the current iteration of a loop. It can be used with repeat, while, and for loops, as well as on and with blocks.

let x = 0
while x < 5 {
x += 1
if x == 3 {
continue
}
Transmission { %(x) }
}
// Outputs 1, 2, 4, 5

surprise

A surprise block is used to randomly choose between multiple options. Only one of the options will be executed.

The simplest form of a surprise block is a list of options. All options are given equal chance of being selected.

surprise {
ShieldPowerup,
TriblastPowerup,
}

It is possible to assign weights to each option to control the likelihood of each option being selected.

surprise {
2 => TriblastPowerup,
1 => ShieldPowerup,
}

In this example, the TriblastPowerup is twice as likely to be selected as the ShieldPowerup.