Control flow
if and while
if (x > 0) {
// ...
} else {
// ...
}
while (running) {
if (skip) { continue; }
if (done) { break; }
}
for
A for loop walks over an iterator. it_range(n) gives numbers from 0 to n - 1:
for (i : it_range(10u32)) {
// i is 0..9
}
You can also get the index:
for (x, index : it_range(10u32)) {
}
A for loop is a short way to write this:
{
__it := it_range(10u32);
defer iterator_end(__it.^);
while (iterator_next(__it.^)) {
x := iterator_current(__it.^);
// body
}
}
So to make your own iterator,
write your own iterator_next, iterator_current and iterator_end functions for your iterator type.
iterator_end runs when the loop ends, also on break and return. Free the iterator’s resources there.
switch
switch (value) {
case 1, 2:
// value is 1 or 2
case 3:
// value is 3
case:
// all other values
}
There is no fall through. Each case stops at the next case.
The default case: is required, unless you switch over an enum:
switch (MOX_OS) {
case .Windows:
println("windows");
case .Linux:
println("linux");
case:
println("other");
}
defer
defer runs an expression at the end of the current scope. Use it to free things right next to where you made them:
fn show_window() {
InitWindow(800, 600, "hello");
defer CloseWindow();
// ... CloseWindow() runs here, when the function ends
}
If there are many defers, they run in reverse order.