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

คุ้มครอง

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

Groups early-exit guard checks before the main body of a function.

Aliases: guard

Syntax: คุ้มครอง <block>

Category#

control-flow

Examples#

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

Groups early-exit guard checks before the main body of a function.

# =============================================================================
# คุ้มครอง — Groups early-exit guard checks before the main body of a function.
# =============================================================================
#
# What this teaches:
#   • Guard blocks — `คุ้มครอง { ถ้า <conditio> { <ทางออก> } }` groups early-exit checks before a function's main logic.
#   • Separation of concerns — guard blocks keep preconditions clearly separated from the main body.
#   • Multiple guards — multiple `ถ้า` conditions in one คุ้มครอง block, each with its own exit path.
#
# Common mistakes:
#   • Using `คุ้มครอง` outside a function — guard blocks are only valid inside ฟังก์ชัน bodies.
#
# See also: ถ้า, คืน
# =============================================================================

# คุ้มครอง — guard blocks for early exit
#
# คุ้มครอง { ถ้า <conditio> { <ทางออก> } }
#
# GRAMMAR:
#   custodiStmt :← 'คุ้มครอง' '{' stmt* '}'
#
# EXPECTED OUTPUT:
#   Division, clamping, and range-validation scalar results.

fn divide(int a, int b) → int {
    guard {
        if b ≡ 0 {
            return 0
        }
    }
    return a / b
}

fn tracta(int x) → int {
    guard {
        if x ≺ 0 {
            return -1
        }
        if x ≻ 100 {
            return -1
        }
    }

    # Main logic, clearly separated from คุ้มครอง
    return x * 2
}

fn stringe(int value, int minimum, int maximum) → int {
    guard {
        if minimum ≻ value {
            return minimum
        }
        if maximum ≺ value {
            return maximum
        }
    }
    return value
}

main {
    # Guard returns 0 instead of dividing by zero
    print divide(10, 2)
    print divide(10, 0)

    # Out-of-range inputs short-circuit to -1
    print tracta(50)
    print tracta(-10)
    print tracta(150)
    print stringe(5, 0, 10)
    print stringe(-5, 0, 10)
    print stringe(15, 0, 10)
}

Expected output:

5
0
100
-1
-1
5
0
10