Kết xuấtvi

nếu

Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.

Starts a conditional branch that runs when its condition is true.

Aliases: if

Syntax: nếu <condition> <block>

Category#

control-flow

Examples#

radix/corpus/si/nidificatus.fab (canonical · keyword)#

Starts a conditional branch that runs when its condition is true.

# =============================================================================
# nếu — Starts a conditional branch that runs when its condition is true.
# =============================================================================
#
# What this teaches:
#   • Nested conditionals — `nếu` blocks can contain further `nếu`/`khác`
#     branches for multi-level decision trees
#   • Demonstrates a login + permission gate: outer `nếu` checks authentication,
#     inner `nếu` checks authorization
#
# Common mistakes:
#   • TODO: deeply nesting conditionals instead of using `nếukhôngthì` chains or
#     guard returns
#
# See also: nếukhôngthì, khác, do_đó
# =============================================================================

# Nested nếu (if) conditionals
#
# nếu <cond> { <body> }                    -- inner branch inside outer arm
# khác { <body> }
#
# GRAMMAR:
#   ifStmt :← 'nếu' expr block ('khác' block)?
#
# EXPECTED OUTPUT:
#   Access granted when intravit and licet are both đúng.

main {
    # logged in
    const bool intravit ← true
    # has permission
    const bool licet ← true
    if intravit {
        # Inner branch: permission check after login succeeds
        if licet {
            print "Access granted"
        }
        else {
            print "Permission denied"
        }
    }
    else {
        # outer else: not logged in
        print "Please log in"
    }
}

Expected output:

Access granted

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

Starts a conditional branch that runs when its condition is true.

test "si runs its body on a verum condition" {
    const int x ← 10
    var bool maior ← false
    if x ≻ 5 {
        maior ← true
    }
    assert maior
}

test "si skips its body on a falsum condition" {
    const int x ← 10
    var bool maior ← false
    if x ≻ 20 {
        maior ← true
    }
    assert not maior
}

test "si block body runs every statement" {
    const int aetas ← 25
    var int gradus ← 0
    if aetas ≥ 18 {
        gradus ← gradus + 1
        gradus ← gradus + 1
    }
    assert gradus ≡ 2
}
# =============================================================================
# nếu — Starts a conditional branch that runs when its condition is true.
# =============================================================================
#
# What this teaches:
#   • Basic conditional execution — `nếu <condition> { body }` runs the body
#     only when the condition evaluates to `đúng`
#   • Multiple independent `nếu` blocks can be chained; each evaluates its own
#     condition independently
#
# Common mistakes:
#   • Using a non-`logic` expression as the `nếu` condition (SEM011), or using `∴` instead of `do_đó` for a single-statement body.
#
# See also: nếukhôngthì, khác, do_đó
# =============================================================================

# Basic nếu (if) conditionals
#
# nếu <condition> { <body> }   -- truthy branch
#
# GRAMMAR:
#   ifStmt :← 'nếu' expr block
#
# EXPECTED OUTPUT:
#   x maior quam 5, Adult, Can vote (si.expected).

main {
    # Simple truthy branch — only the first condition matches
    const int x ← 10
    if x ≻ 5 {
        print "x maior quam 5"
    }
    if x ≻ 20 {
        # skipped: 10 ≯ 20
        print "x maior quam 20"
    }

    # Block body: multiple statements run together when condition holds
    const int aetas ← 25
    if aetas ≥ 18 {
        print "Adult"
        print "Can vote"
    }
}

Expected output:

x maior quam 5
Adult
Can vote