Conditions and Loops
There are several ways to handle conditional control flow in QangLang.
if and else Statements
Section titled “if and else Statements”if statements can be used to run code only when the condition is true.
if (true) { // Do something}if can be combined with else to run code when the condition is false.
if (false) { // This won't run} else { // This will run.}if and else can be chained together to handle multiple conditions.
if (first_condition) { // Do the thing for the first condition} else if (second_condition) { // Do the thing for the second condition}and and or
Section titled “and and or”The logical operators and and or can be used to apply multiple conditions.
if (first_condition and second_condition) { // Is true if both first_condition and second_condition are true // Do something} else if (first_condition or second_condition) { // Is true if first_condition or second_condition is true // Do something else}Ternary and when Expressions
Section titled “Ternary and when Expressions”For expressions, the ternary expression can be used to handle simple if else logic.
var absolute_value = value >= 0 ? value : value * -1;For more complicated conditional logic in an expression, a when expression can be used. For the conditional when expression, the left side is an expression that if evaluated as truthy, will evaluate the expression on the right.
var sign = when { value > 0 => "positive", value == 0 => "zero", value < 0 => "negative",}when expressions can also supply a value to match against. The match when expression checks the left side for equality with the value. If equal, then the expresson on the right will be evaluated.
fn get_hex_color(color) { return when (color) { "red" => "#FF0000", "green" => "#00FF00", "blue" => "#0000FF", "black" => "#000000", else => "#FFFFFF" }}The is operator can also be used to match against the type. If the value matches the type on the left, the expression on the right will be evaluated.
var value = when (result) { is Ok => result.unwrap() is Err => "default"}Sometimes a simple expression may not be enough. In those instances, an Immediately executed lambda can be used to wrap the statements.
var value = when (result) { is Ok => result.unwrap() is Err => () -> { // Do something on this line // Then do something on this line }()}while Loops
Section titled “while Loops”A while loop runs its body continuously until the provided condition is false. If the condition is never true, then the body will never run.
var run_loop = true;while (run_loop) { run_loop = false;}for Loops
Section titled “for Loops”Like while loops, for loops run their body until the provided condtion is false.
var arr = array_of(3);for ( var i = 0; // initializer i < arr.length(); // condition i += 1 // afterthought ) { println(arr[i]); // body}// 0// 1// 2for Loop Execution Order
Section titled “for Loop Execution Order”- The initializer, if provided, is initialized. The initializer can be either a variable declaration, or any expression of arbitrary complexity.
- The condition is checked, if evaluated to
true, the loop terminates. - The body is evaluated.
- If provided, the afterthought is evaluated.
break and continue in Loops
Section titled “break and continue in Loops”See Return and Jumps for more information on break and continue
Iterators
Section titled “Iterators”If the value can be converted to an Iterator, the for_each method can be used to iterate through the value.
Range(0, 3).for_each((num) -> { println(num);});