當
Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.
Repeats a block while a condition remains true.
Aliases: while
Syntax: 當 <condition> <block|則 statement>
Category#
control-flow
Related#
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
}
# =============================================================================
# 當 — Repeats a block while a condition remains true.
# =============================================================================
#
# What this teaches:
# • While loop — `當 <condition> { <body> }` repeats a block as long as the condition is true.
# • Loop variable mutation — mutable `變值` bindings allow counter-based loop control.
# • Ascending and descending loops — counting up from 0 or down from N until the condition is 假.
#
# Common mistakes:
# • Using `中斷` or `繼續` outside a loop — `繼續` needs an enclosing loop (`當`, `遍歷`, `執行 … 當`) in the same function (SEM031); `中斷` also accepts a plain `執行` block (SEM030).
#
# See also: 若, 則, 執行
# =============================================================================
# 當 — while loop
#
# 當 <condition> { <body> }
#
# GRAMMAR:
# loopStmt :← '當' expr '{' stmt* '}'
#
# EXPECTED OUTPUT:
# dum.expected
main {
# Ascending counter: 變值 so the loop variable can mutate
var int computus ← 0
while computus ≺ 5 {
print computus
computus ← computus + 1
}
# Descending loop: condition becomes 假 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!