Ruby if-else语句

Ruby if else语句用于测试条件。 Ruby中有各种各样的if语句。

  • if语句
  • if-else语句
  • if-else-if(elsif)语句
  • 三元(缩写if语句)语句

1. Ruby if语句

Ruby if语句测试条件。 如果conditiontrue,则执行if语句。

语法:

if (condition)  
//code to be executed  
end

流程示意图如下所示 -

代码示例:

a = gets.chomp.to_i   
if a >= 18   
  puts "You are eligible to vote."   
end

将上面代码保存到文件:if-statement.rb中,执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby if-statement.rb
90
You are eligible to vote.

F:\worksp\ruby>

2. Ruby if else语句

Ruby if else语句测试条件。 如果conditiontrue,则执行if语句,否则执行block语句。

语法:

if(condition)  
    //code if condition is true  
else  
//code if condition is false  
end

流程示意图如下所示 -

代码示例:

a = gets.chomp.to_i   
if a >= 18   
  puts "You are eligible to vote."   
else   
  puts "You are not eligible to vote."   
end

将上面代码保存到文件:if-else-statement.rb中,执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby if-else-statement.rb
80
You are eligible to vote.

F:\worksp\ruby>ruby if-else-statement.rb
15
You are not eligible to vote.

F:\worksp\ruby>

3. Ruby if else if(elsif)

Ruby if else if 语句测试条件。 如果conditiontrue则执行if语句,否则执行block语句。

语法:

if(condition1)  
//code to be executed if condition1is true  
elsif (condition2)  
//code to be executed if condition2 is true  
else (condition3)  
//code to be executed if condition3 is true  
end

流程示意图如下所示 -

示例代码 -

a = gets.chomp.to_i   
if a <50   
  puts "Student is fail"   
elsif a >= 50 && a <= 60   
  puts "Student gets D grade"   
elsif a >= 70 && a <= 80   
  puts "Student gets B grade"   
elsif a >= 80 && a <= 90   
  puts "Student gets A grade"    
elsif a >= 90 && a <= 100   
  puts "Student gets A+ grade"    
end

将上面代码保存到文件:if-else-if-statement.rb中,执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby if-else-if-statement.rb
25
Student is fail

F:\worksp\ruby>ruby if-else-if-statement.rb
80
Student gets B grade

F:\worksp\ruby>

4. Ruby三元语句

在Ruby三元语句中,if语句缩短。首先,它计算一个表达式的truefalse值,然后执行一个语句。

语法:

test-expression ? if-true-expression : if-false-expression

示例代码

var = gets.chomp.to_i;   
a = (var > 3 ? true : false);    
puts a

将上面代码保存到文件:ternary-statement.rb中,执行上面代码,得到以下结果 -

F:\worksp\ruby>ruby ternary-operator.rb
Ternary operator
5
2

F:\worksp\ruby>

上一篇: Ruby数据类型 下一篇: Ruby Case语句