If blocks

The following syntax allows you to create a series of conditions followed by some code to execute, the first condition met will be the one to trigger it's matching block:

if condition_1{
    statements_1
} else if condition_2 {
    statements_2
} else if condition_3 {
    statements_3
...
} else {
    statements_n
}

for example:

if number >= 10{
    println("This number has at least two digits")
} else {
    println("This number is either negative or has less than two digits")
}

The else statements are not mandatory, making the following also valid:

if number >= 10{
    println("This number has at least two digits")
}

Optimizations

When the conditions for an if block are constant, they might be inlined to lower the performance and memory costs of both executing and storing lines that will never be visited, the following examples will be transformed:

Input Moon Script Optimization
if true{
    println("Always executed")
} else{
    println("Never executed")
}
println("Always executed")
if true {
    println("Always executed")
} else if n>5{
    println("Never executed")
} else{
    println("Never executed")
}
println("Always executed")
if n>5 {
    println("Only executed when n>5")
} else if true{
    println("Only executed when not n>5")
} else{
    println("Never executed")
}
- if n isn't known when compiling this script:
if n>5 {
    println("Only executed when n>5")
} else if true{
    println("Only executed when not n>5")
}

- if n is known when compiling this script and n>5:

println("Only executed when n>5")

- if n is known when compiling this script and not n>5:

println("Only executed when not n>5")