← All examples

And then or else

and.then / or.else: the right side is worked out only when the left has not already decided — "A, and then (only if A held) B", "A, or else (only if A failed) B".

and_then_or_else.zg
# `and.then` / `or.else` (owner, 2026-09-25): the right side is worked out only when the
# left has not already decided — "A, and then (only if A held) B", "A, or else (only if
# A failed) B". Plain `and` / `or` always work out both (examples/and_runs_both.zg).
fn loud ['name': str.utf8, 'answer': bool] -> bool {
    std::print.useful["checked ", 'name', newln];
    return 'answer';
}
MAIN {
    let 'xs' := [3, 5, 8];
    let 'i': uint64 = 7;
    # 'i' is past the end, so the list is never read
    if 'i' at_most list.length[lend 'xs'] and.then 'xs'[pos 'i'] equal_to 5 { std::print.useful["found", newln]; }
    let 'n': int32 = 0;
    if 'n' equal_to 0 or.else 100 div.floor 'n' < 2 { std::print.useful["small or nothing", newln]; }
    let 'a' := loud["first", false] and.then loud["second", true];
    let 'b' := loud["third", true] or.else loud["fourth", true];
    let 'c' := loud["fifth", true] and.then loud["sixth", false];
    std::print.useful['a', " ", 'b', " ", 'c', newln];
}
Output
small or nothing
checked first
checked third
checked fifth
checked sixth
false true false