如果
Translation status: 简体中文 reader-locale proof. Term names and code fences follow the zh-Hans pack; supporting prose may still be English.
Starts a conditional branch that runs when its condition is true.
Aliases: if
Syntax: 如果 <condition> <block>
Category#
control-flow
Related#
Examples#
radix/corpus/si/nidificatus.fab (canonical · keyword)#
Starts a conditional branch that runs when its condition is true.
# =============================================================================
# 如果 — Starts a conditional branch that runs when its condition is true.
# =============================================================================
#
# What this teaches:
# • Nested conditionals — `如果` blocks can contain further `如果`/`否则`
# branches for multi-level decision trees
# • Demonstrates a login + permission gate: outer `如果` checks authentication,
# inner `如果` checks authorization
#
# Common mistakes:
# • TODO: deeply nesting conditionals instead of using `否则如果` chains or
# guard returns
#
# See also: 否则如果, 否则, 则
# =============================================================================
# Nested 如果 (if) conditionals
#
# 如果 <cond> { <body> } -- inner branch inside outer arm
# 否则 { <body> }
#
# GRAMMAR:
# ifStmt :← '如果' expr block ('否则' block)?
#
# EXPECTED OUTPUT:
# Access granted when intravit and licet are both 真.
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
}
# =============================================================================
# 如果 — Starts a conditional branch that runs when its condition is true.
# =============================================================================
#
# What this teaches:
# • Basic conditional execution — `如果 <condition> { body }` runs the body
# only when the condition evaluates to `真`
# • Multiple independent `如果` blocks can be chained; each evaluates its own
# condition independently
#
# Common mistakes:
# • Using a non-`布尔` expression as the `如果` condition (SEM011), or using `∴` instead of `则` for a single-statement body.
#
# See also: 否则如果, 否则, 则
# =============================================================================
# Basic 如果 (if) conditionals
#
# 如果 <condition> { <body> } -- truthy branch
#
# GRAMMAR:
# ifStmt :← '如果' 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