Renderingen-US

while

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Repeats a block while a condition remains true.

Aliases: while

Syntax: while <condition> <block|then statement>

Category#

control-flow

Examples#

radix/corpus/dum/dum.fab (canonical · keyword)#

Repeats a block while a condition remains true.

test "dum counts up to its condition" {
    var int computus ← 0
    while computus ≺ 5 {
        computus ← computus + 1
    }
    assert computus ≡ 5
}

test "dum counts down to zero" {
    var int reliquum ← 3
    while reliquum ≻ 0 {
        reliquum ← reliquum - 1
    }
    assert reliquum ≡ 0
}
# =============================================================================
# while — Repeats a block while a condition remains true.
# =============================================================================
#
# What this teaches:
#   • While loop — `while <condition> { <body> }` repeats a block as long as the condition is true.
#   • Loop variable mutation — mutable `var` bindings allow counter-based loop control.
#   • Ascending and descending loops — counting up from 0 or down from N until the condition is false.
#
# Common mistakes:
#   • Using `break` or `continue` outside a loop — `continue` needs an enclosing loop (`while`, `for`, `do … while`) in the same function (SEM031); `break` also accepts a plain `do` block (SEM030).
#
# See also: if, then, do
# =============================================================================

# while — while loop
#
# while <condition> { <body> }
#
# GRAMMAR:
#   loopStmt :← 'while' expr '{' stmt* '}'
#
# EXPECTED OUTPUT:
#   dum.expected

main {
    # Ascending counter: var so the loop variable can mutate
    var int computus ← 0
    while computus ≺ 5 {
        print computus
        computus ← computus + 1
    }

    # Descending loop: condition becomes false at 0
    var int reliquum ← 3
    while reliquum ≻ 0 {
        print "reliquum: §"(reliquum)
        reliquum ← reliquum - 1
    }
    print "perfectum!"
}

Expected output:

0
1
2
3
4
reliquum: 3
reliquum: 2
reliquum: 1
perfectum!