trong_khi
Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.
Repeats a block while a condition remains true.
Aliases: while
Syntax: trong_khi <condition> <block|do_đó 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
}
# =============================================================================
# trong_khi — Repeats a block while a condition remains true.
# =============================================================================
#
# What this teaches:
# • While loop — `trong_khi <condition> { <body> }` repeats a block as long as the condition is true.
# • Loop variable mutation — mutable `biến` bindings allow counter-based loop control.
# • Ascending and descending loops — counting up from 0 or down from N until the condition is sai.
#
# Common mistakes:
# • Using `dừng` or `tiếp` outside a loop — `tiếp` needs an enclosing loop (`trong_khi`, `lặp`, `làm … trong_khi`) in the same function (SEM031); `dừng` also accepts a plain `làm` block (SEM030).
#
# See also: nếu, do_đó, làm
# =============================================================================
# trong_khi — while loop
#
# trong_khi <condition> { <body> }
#
# GRAMMAR:
# loopStmt :← 'trong_khi' expr '{' stmt* '}'
#
# EXPECTED OUTPUT:
# dum.expected
main {
# Ascending counter: biến so the loop variable can mutate
var int computus ← 0
while computus ≺ 5 {
print computus
computus ← computus + 1
}
# Descending loop: condition becomes sai 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!