if-else-if-else
if-else-if-else is the most basic decision-making tool in programming. Its logic is very intuitive—if a condition holds, execute a block of code; otherwise, check the next condition; if none hold, take the default path.
Basic Syntax
The definitions of if expressions and if statements in the syntax specification are exactly the same:
if Expr Block ('else' 'if' Expr Block)* ('else' Block)?In everyday language: if comes first, followed by a condition expression and a block of code, then zero or more else if condition block clauses, and finally an optional else block.
The simplest form—only if:
if temperature > 30 {
print("天热了,开空调吧")
}Add else:
if is_raining {
print("带伞")
} else {
print("不用带伞")
}Use else if for multiple conditions:
score = 85
if score >= 90 {
print("优秀")
} else if score >= 80 {
print("良好")
} else if score >= 60 {
print("及格")
} else {
print("需要努力")
}if Is an Expression
This is one of the most important features of YaoXiang control flow: if can be used as an expression to compute a value.
// if expression: the value of each branch is assigned to result
x = 42
result = if x > 0 {
"正数"
} else if x < 0 {
"负数"
} else {
"零"
}
// result is now one of the three possible branch valuesWhen if is used as an expression, the return value types of all branches must be identical:
score = 88
// All branches return String, so their types are consistent
grade = if score >= 90 {
"A"
} else if score >= 80 {
"B"
} else if score >= 60 {
"C"
} else {
"D"
}
print(grade) // "B"Within the block of each branch, the value of the final expression is the branch's return value. You can also use return explicitly, but usually writing the expression directly in the branch is sufficient.
age = 15
// Write the expression directly—recommended
category = if age < 18 { "未成年" } else { "成年" }
// You can also use an explicit return—the result is the same
category2 = if age < 18 {
return "未成年"
} else {
return "成年"
}If you use if only for conditional execution and do not need a value, it is an ordinary statement—and it is fully compatible with the expression form.
Nested if
You can write another if inside an if to handle multiple levels of conditional logic:
age = 25
has_ticket = true
if age >= 18 {
if has_ticket {
print("欢迎入场")
} else {
print("请先购票")
}
} else {
print("未成年人需家长陪同")
}When expressions are nested, YaoXiang has no "dangling else" ambiguity like C—every else always belongs to the nearest unmatched if.
Combining Conditions with Boolean Operators
You can combine multiple checks in a condition with and and or, and negate them with the unary prefix !. ⚠️ YaoXiang does not have a not keyword (the reserved words are only and / or); writing not is_banned produces the parse error E0010, so you must write !is_banned.
username = "admin"
password = "123456"
role = "admin"
is_banned = false
is_vip = true
age = 25
// and: both conditions must be true
if username == "admin" and password == "123456" {
print("登录成功")
}
// or: either condition may be true
if role == "admin" or role == "moderator" {
print("有管理权限")
}
// !: negates the condition
if !is_banned {
print("允许发言")
}
// Combining conditions
if (age >= 18 and age <= 60) or is_vip {
print("可以参加活动")
}In terms of operator precedence, ! has higher precedence than and, and and has higher precedence than or. When in doubt, add parentheses to make the intent clearer.
Summary
| Key | Description |
|---|---|
| Basic structure | if condition { ... } else if condition { ... } else { ... } |
| else if | YaoXiang uses else if to implement multiple branches |
| Expression | if can return a value, and all branches must have the same type |
| Branch return value | The value of the last expression in a branch block is the return value |
| Nesting | You can nest an if inside another if, with no dangling-else ambiguity |
| Boolean operations | and and or combine conditions, while ! negates |
In the next chapter, you will learn about the for loop—the standard way to iterate over collections and ranges.
