条件语句 #
一、if语句 #
1.1 基本语法 #
julia
if condition
statements
end
1.2 简单示例 #
julia
x = 10
if x > 0
println("x is positive")
end
1.3 if-else语句 #
julia
x = -5
if x > 0
println("x is positive")
else
println("x is not positive")
end
1.4 if-elseif-else语句 #
julia
x = 0
if x > 0
println("x is positive")
elseif x < 0
println("x is negative")
else
println("x is zero")
end
1.5 多个elseif #
julia
score = 85
if score >= 90
println("A")
elseif score >= 80
println("B")
elseif score >= 70
println("C")
elseif score >= 60
println("D")
else
println("F")
end
二、条件表达式 #
2.1 if表达式 #
if语句可以返回值:
julia
x = 10
result = if x > 0
"positive"
else
"non-positive"
end
println(result)
2.2 多行表达式 #
julia
x = 10
result = if x > 0
y = x * 2
y + 1
else
0
end
三、三元运算符 #
3.1 基本语法 #
julia
condition ? value_if_true : value_if_false
3.2 示例 #
julia
x = 5
result = x > 0 ? "positive" : "non-positive"
3.3 嵌套三元 #
julia
x = 0
result = x > 0 ? "positive" : x < 0 ? "negative" : "zero"
3.4 与if-else对比 #
julia
x = 5
result = x > 0 ? "positive" : "non-positive"
result = if x > 0
"positive"
else
"non-positive"
end
四、短路条件执行 #
4.1 && 短路 #
julia
x = 5
x > 0 && println("x is positive")
x < 0 && println("This won't print")
4.2 || 短路 #
julia
x = 5
x < 0 || println("x is non-negative")
x > 0 || println("This won't print")
4.3 实际应用 #
julia
function safe_divide(a, b)
b == 0 && return nothing
return a / b
end
function get_default(dict, key, default)
haskey(dict, key) && return dict[key]
return default
end
五、条件语句嵌套 #
5.1 嵌套if #
julia
x, y = 5, 3
if x > 0
if y > 0
println("Both positive")
else
println("x positive, y not")
end
else
println("x not positive")
end
5.2 使用逻辑运算符简化 #
julia
x, y = 5, 3
if x > 0 && y > 0
println("Both positive")
elseif x > 0
println("x positive, y not")
else
println("x not positive")
end
六、模式匹配 #
6.1 isa检查 #
julia
x = 10
if x isa Int
println("Integer: $x")
elseif x isa Float64
println("Float: $x")
elseif x isa String
println("String: $x")
end
6.2 类型分发 #
julia
function process(x)
if x isa Int
x * 2
elseif x isa Float64
x / 2
elseif x isa String
uppercase(x)
else
x
end
end
七、实践练习 #
7.1 练习1:成绩等级 #
julia
function grade(score)
if score >= 90
"A"
elseif score >= 80
"B"
elseif score >= 70
"C"
elseif score >= 60
"D"
else
"F"
end
end
grade(85)
grade(72)
grade(55)
7.2 练习2:闰年判断 #
julia
function is_leap_year(year)
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
end
is_leap_year(2000)
is_leap_year(1900)
is_leap_year(2024)
7.3 练习3:数字分类 #
julia
function classify_number(n)
if n == 0
"Zero"
elseif n > 0
if n % 2 == 0
"Positive Even"
else
"Positive Odd"
end
else
if n % 2 == 0
"Negative Even"
else
"Negative Odd"
end
end
end
classify_number(10)
classify_number(-5)
classify_number(0)
7.4 练习4:简单计算器 #
julia
function calculator(a, op, b)
if op == "+"
a + b
elseif op == "-"
a - b
elseif op == "*"
a * b
elseif op == "/"
b == 0 ? error("Division by zero") : a / b
else
error("Unknown operator: $op")
end
end
calculator(10, "+", 5)
calculator(10, "/", 2)
八、总结 #
本章我们学习了:
- if语句:if-elseif-else结构
- 条件表达式:if语句返回值
- 三元运算符:简洁的条件表达式
- 短路执行:&&和||的条件执行
- 嵌套条件:多层条件判断
接下来让我们学习Julia的循环语句!
最后更新:2026-03-27