Control Flow
If
src/structs.duck
fn main() {
if (3 == 3) {
std::io::println("3 is equal to 3");
}
if (3 == 3 and 5 == 5) {
std::io::println("and worked");
}
if (3 == 4 or 6 == 6) {
std::io::println("or worked");
}
// if can be used as an expression
const msg = if (1.to_string() == "1") {
"i am the message for true"
} else {
"this is the false message"
};
std::io::println(msg);
}
For loops
For loops work on std::col::Iter and provide access to each element
src/structs.duck
fn main() {
const list = std::col::ArrayList::from_array(["first elem", "second elem"]);
for (elem in list.iter()) {
std::io::println(elem.to_string());
}
for (elem in list.iter().rev()) { // get every element in reverse
std::io::println(elem.to_string());
}
}
While loops
src/structs.duck
fn main() {
let count = 0;
while (count < 5) {
std::io::println(f"Currently at {count.to_string()}");
if (count == 3) {
break;
}
if (count == 4) {
continue;
}
count = count + 1;
}
}