การเรนเดอร์th-TH

ขณะ

Translation status: ภาษาไทย reader-locale proof. Term names and code fences follow the th-TH pack; supporting prose may still be English.

Repeats a block while a condition remains true.

Aliases: while

Syntax: ขณะ <condition> <block|ดังนั้น 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
}
# =============================================================================
# ขณะ — 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!