条件语句 #
一、if语句基础 #
1.1 基本语法 #
mojo
def main():
let age = 20
if age >= 18:
print("成年人")
main()
1.2 if-else语句 #
mojo
def main():
let age = 15
if age >= 18:
print("成年人")
else:
print("未成年人")
main()
1.3 if-elif-else语句 #
mojo
def main():
let score = 85
if score >= 90:
print("优秀")
elif score >= 80:
print("良好")
elif score >= 70:
print("中等")
elif score >= 60:
print("及格")
else:
print("不及格")
main()
二、条件表达式 #
2.1 比较运算符 #
mojo
def main():
let a = 10
let b = 20
if a < b:
print("a小于b")
if a <= b:
print("a小于等于b")
if a == b:
print("a等于b")
if a != b:
print("a不等于b")
main()
2.2 逻辑运算符 #
mojo
def main():
let age = 25
let has_license = True
if age >= 18 and has_license:
print("可以驾驶")
if age < 18 or not has_license:
print("不能驾驶")
main()
2.3 成员运算符 #
mojo
def main():
let fruits = ["apple", "banana", "orange"]
let fruit = "apple"
if fruit in fruits:
print(f"{fruit} 在列表中")
if "grape" not in fruits:
print("葡萄不在列表中")
main()
2.4 身份运算符 #
mojo
def main():
let a = None
let b = "Hello"
if a is None:
print("a是None")
if b is not None:
print("b不是None")
main()
三、嵌套条件 #
3.1 嵌套if语句 #
mojo
def main():
let age = 25
let has_id = True
if age >= 18:
if has_id:
print("可以进入")
else:
print("需要身份证")
else:
print("未成年人禁止进入")
main()
3.2 避免深层嵌套 #
mojo
def main():
let age = 25
let has_id = True
if age < 18:
print("未成年人禁止进入")
elif not has_id:
print("需要身份证")
else:
print("可以进入")
main()
四、三元表达式 #
4.1 基本语法 #
mojo
def main():
let age = 20
let status = "成年" if age >= 18 else "未成年"
print(status)
main()
4.2 嵌套三元表达式 #
mojo
def main():
let score = 85
let grade = "A" if score >= 90 else "B" if score >= 80 else "C"
print(grade)
main()
4.3 实际应用 #
mojo
def main():
let numbers = [1, 2, 3, 4, 5]
for num in numbers:
let desc = "偶数" if num % 2 == 0 else "奇数"
print(f"{num}是{desc}")
main()
五、模式匹配 #
5.1 基本匹配 #
mojo
def main():
let value = 2
match value:
case 1:
print("一")
case 2:
print("二")
case 3:
print("三")
case _:
print("其他")
main()
5.2 多值匹配 #
mojo
def main():
let value = 5
match value:
case 1 | 2 | 3:
print("小数字")
case 4 | 5 | 6:
print("中等数字")
case _:
print("其他")
main()
5.3 条件匹配 #
mojo
def main():
let score = 85
match score:
case s if s >= 90:
print("优秀")
case s if s >= 80:
print("良好")
case s if s >= 60:
print("及格")
case _:
print("不及格")
main()
5.4 类型匹配 #
mojo
def main():
let value: Variant = 42
match value:
case Int(x):
print(f"整数: {x}")
case Float(x):
print(f"浮点数: {x}")
case String(x):
print(f"字符串: {x}")
main()
六、条件最佳实践 #
6.1 简化条件 #
mojo
def main():
let is_valid = True
if is_valid:
print("有效")
main()
6.2 提前返回 #
mojo
def process(value: Int) -> String:
if value < 0:
return "无效"
if value == 0:
return "零"
return "正数"
def main():
print(process(-1))
print(process(0))
print(process(10))
main()
6.3 使用字典替代多重if #
mojo
def main():
let operation = "add"
let a = 10
let b = 5
let operations = {
"add": lambda x, y: x + y,
"sub": lambda x, y: x - y,
"mul": lambda x, y: x * y,
}
if operation in operations:
let result = operations[operation](a, b)
print(result)
main()
七、常见陷阱 #
7.1 悬空else #
mojo
def main():
let a = 10
let b = 20
if a > 5:
if b > 25:
print("条件1")
else:
print("条件2")
main()
7.2 链式比较 #
mojo
def main():
let x = 15
if 10 < x < 20:
print("x在10到20之间")
main()
7.3 短路求值 #
mojo
def main():
let a = 0
if a != 0 and 10 / a > 1:
print("不会执行")
main()
八、总结 #
本章学习了:
- if-elif-else语句
- 条件表达式
- 逻辑运算符
- 嵌套条件
- 三元表达式
- 模式匹配
- 最佳实践
下一章,我们将学习循环语句。
最后更新:2026-03-27