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 n isn't known when compiling this script:
- if n is known when compiling this script and n>5:
- if n is known when compiling this script and not n>5:
|