Skip to content

Control Flow#

repeat n times#

repeat n {
    # code
}

repeat until condition#

until condition == true {
    # code
}

forever loop#

forever {
    # code
}

if#

if condition {
    # code
}

if else#

if condition {
    # code
}
else {
    # code
}

if elif#

if condition {
    # code
}
elif condition {
    # code
}

Ternary Expressions#

Embed conditional logic directly inside expressions.

Syntax#

if (<condition>) <true_value> else <false_value>

Examples#

say if (score > 100) "winner" else "loser";

Ternaries can be nested — an else branch can itself be a ternary:

say if (condition_A) "A_true"
    else if (condition_B) "B_true"
    else "false";

When used as a condition, a ternary must be wrapped in parentheses:

say if (if (condition_A) true else false) "yes" else "no";

Compilation#

Ternaries are desugared at compile time into if/else branches. Each branch receives a copy of the surrounding statement with the ternary substituted for the appropriate value.