首页 > 代码库 > Ruby_day[1]控制流(if, unless)

Ruby_day[1]控制流(if, unless)

summary:

  1: if, elsif, else.注意结束if时后面加end

  2: unless, else

 

  • How to use if, else, and elsif
  • How to use comparators / relational operators like ==(相等), !=(不等), <, <=, >, and >=

How to use boolean / logical operators like &&(与), ||(或), and !(非)

1 a = 102 b = 113 if a < b4   print "a is less than b!"5 elsif b > a6   print "b is less than a!"7 else8   print "b is equal to a!"9 end

 

1 problem = false2 print "Good to go!" unless problem3 #除非有问题(problem=true时不打出来),否则打印出来

 

多个逻辑运算结合,用括号 括起来

1 ( 1 == 1 ) && ( 2 == 2 )  // true2 ( 1 == 2 ) || ( 2 == 2 ) // true3 !( false ) // true

 

 

Ruby_day[1]控制流(if, unless)