Skip to main content

If statements

if statements allow your program to perform different actions depending on a condition. The condition can be any expression with a boolean type (true or false).

If-then​

In the example below, the condition we test is age < 8. If that is true, then we print a warning message. Otherwise the code between then and end if is skipped.

module main
func show_notice(age: int): int
if age < 8 then
# This code only runs when age is less than 8
console::print("You are too young to ride.")
end if

# This code always runs
console::print("{n}")
end func

func start()
# Minimal initialization of the Hybrix framework console
console::init(io::tilemap_a)
var font_layer: tile_layer
font_layer <- new tile_layer(io::tilemap_a, io::tileset_a_addresses)
font_layer.load_tileset(art::fonts[0])

main::show_notice(5)
end func
end module

Else​

Instead of skipping to end if, we can instead use else to provide an alternative behavior:

module main
func show_notice(age: int): int
if age < 8 then
# Only runs when age < 8
console::print("You are too young to ride.")
else
# Only runs when age >= 8
console::print("You are old enough to ride.")
end if

# This code always runs
console::print("{n}")
end func

func start()
# Minimal initialization of the Hybrix framework console
console::init(io::tilemap_a)
var font_layer: tile_layer
font_layer <- new tile_layer(io::tilemap_a, io::tileset_a_addresses)
font_layer.load_tileset(art::fonts[0])

main::show_notice(5)
end func
end module

Elsif​

We can use elsif to consider multiple conditions:

module main
func show_notice(age: int): int
if age < 8 then
# Less than 8
console::print("You are too young to ride.")
elsif age <= 12 then
# Between 8 and 12
console::print("Ask your parent for permission.")
elsif age >= 18 then
# 18 or older
console::print("You are too old to ride.")
else
# Between 13 and 17
console::print("You are old enough to ride.")
end if

# This code always runs
console::print("{n}")
end func

func start()
# Minimal initialization of the Hybrix framework console
console::init(io::tilemap_a)
var font_layer: tile_layer
font_layer <- new tile_layer(io::tilemap_a, io::tileset_a_addresses)
font_layer.load_tileset(art::fonts[0])

main::show_notice(5)
end func
end module

You may wonder why we need elsif. Couldn't we just use else and if?

You can, but the end if indentation will accumulate quickly:

. . .
if age < 8 then
# Less than 8
console::print("You are too young to ride.")
else
if age <= 12 then
# Between 8 and 12
console::print("Ask your parent for permission.")
else
if age >= 18 then
# 18 or older
console::print("You are too old to ride.")
else
# Between 13 and 17
console::print("You are old enough to ride.")
end if
end if
end if
. . .

elsif is cleaner and simpler. (Of course elsif could be spelled out as else if, but then beginners might have trouble recognizing which if matches the end if.)

Do​

Programs frequently use very simple if statements, for example:

. . .
if count < 0 then
count <- 0
end if
. . .
. . .
if count > 10 then
return result
end if
. . .

You can save a line of code by using do instead of then. The examples below have identical meaning as above:

. . .
if count < 0
do count <- 0
. . .
. . .
if count > 10
do return result
. . .

The do syntax can only be used with simple statements such as assignments or function calls. The following examples are NOT allowed:

. . .
# ⚠️ ERROR: "loop" is not allowed with "do"
if count < 0
do loop
end loop
. . .
. . .
# ⚠️ ERROR: "if" is not allowed with "do"
if count < 0
do if count > 5
do count <- 0
. . .