1. if...else语句
语法:
if conditional [then]
code...
[elsif conditional [then]
code...]...
[else
code...]
end
例子:
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
x=1
if x > 2
puts "x 大于 2"
elsif x <= 2 and x!=0
puts "x 是 1"
else
puts "无法得知 x 的值"
end
运行结果:
x 是 1
2. unless语句
语法:
unless conditional [then]
code
[else
code ]
end
例子:
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
x=1
unless x>2
puts "x 小于 2"
else
puts "x 大于 2"
end
运行结果:
x 小于 2
3. case语句
语法:
case expression
[when expression [, expression ...] [then]
code ]...
[else
code ]
end
例子:
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
$age = 5
case $age
when 0 .. 2
puts "婴儿"
when 3 .. 6
puts "小孩"
when 7 .. 12
puts "child"
when 13 .. 18
puts "少年"
else
puts "其他年龄段的"
end
运行结果:
小孩