Ruby 條件語句
Ruby 提供了現(xiàn)代語言常見的條件結(jié)構(gòu),在本章節(jié)中我們會學習到如何在 Ruby 中所有可用的條件語句使用方式。
1. if… 語句
if...
語句是條件語句中最基本的,當 if
后的條件表達式得到滿足的時候,執(zhí)行代碼部分,不滿足條件的時候,代碼將被忽略。
實例:
if 10 < 20 then
puts "10 is less than 20"
end
# ---- 輸出結(jié)果 ----
10 is less than 20
解釋:在執(zhí)行時,代碼將顯示字符串“10 is less than 20”,因為 10 < 20
表達式的計算結(jié)果為true
。end
語句標志著if
語句的結(jié)束。
經(jīng)驗:在實際開發(fā)中我們常常省略 then
,得到的結(jié)果是相同的。
實例:
if 10 < 20
puts "10 is less than 20"
end
# ---- 輸出結(jié)果 ----
10 is less than 20
另外,我們還可以將 if
語句的判斷部分后置,這叫做條件后置:
實例:
puts "10 is less than 20" if 10 < 20
# ---- 輸出結(jié)果 ----
10 is less than 20
if
后面的條件表達式可以使用邏輯運算符。
實例:
firstname = 'john'
lastname = "smith"
if firstname == "john" && lastname == "smith"
puts "Hello John!"
end
# ---- 輸出結(jié)果 ----
Hello John!
還有一種跟 if
相反的判斷,名叫 unless
,它和 if
剛好相反,當 unless
后面的條件不滿足的時候,才執(zhí)行代碼部分。
實例:
unless 10 >= 20
puts "10 not bigger than or equal 20"
end
# ---- 輸出結(jié)果 ----
10 not bigger than or equal 20
注意事項:unless
也可以稱為 if not,因為很容易讓人混淆,所以我們盡量不使用它。
2. if…else語句
上面的 if
表達式只提供了條件表達式計算結(jié)果為 true
,的情況,并沒有為結(jié)果為 false
時進行考慮,if...else
語句在這時就起到了作用。
實例:
customerName = "Andrew"
if customerName == "Fred"
puts "Hello Fred!"
else
puts "You're not Fred! Where's Fred?"
end
# ---- 輸出結(jié)果 ----
You're not Fred! Where's Fred?
解釋:當條件表達式為true
的時候,執(zhí)行else
之前的內(nèi)容,當表達式為false
的時候,執(zhí)行else
之后的內(nèi)容。
3. if…elsif…語句
當有許多條件需要判斷的時候需要使用 if...elsif...
語句。
實例:
customerName = "Andrew"
if customerName == "Fred"
puts "Hello Fred!"
elsif customerName == 'Andrew'
puts "Hello Andrew!"
else
puts "You're not my customer."
end
# ---- 輸出結(jié)果 ----
Hello Andrew!
我們也可以在某些情況省略 else
。
實例:
customerName = "Andrew"
if customerName == "Fred"
puts "Hello Fred!"
elsif customerName == 'Andrew'
puts "Hello Andrew!"
end
# ---- 輸出結(jié)果 ----
Hello Andrew!
4. 三元運算符
三元運算符是一種便捷表達條件語句的形式,它的語法如下:
[條件] ? [true expression] : [false expression]
當條件為true
的時候,會執(zhí)行冒號左側(cè)代表真的表達式,當條件為false
的時候,會執(zhí)行冒號右側(cè)代表假的表達式。
實例:
customerName = "Andrew"
puts customerName == "Fred" ? "Hello Fred" : "Who are you?"
# ---- 輸出結(jié)果 ----
Who are you?
5. case 表達式
當我們多次使用if...elsif...
來檢查值的時候,可以使用 case 表達式來替代。
實例:
比如使用 if..elsif...
時表達式為這個亞子:
customerName = "John"
if customerName == "Fred"
puts "Hello Fred!"
elsif customerName == "John"
puts "Hello John!"
elsif customername == "Robert"
puts "Hello Bob!"
else
puts "Who are you?"
end
# ---- 輸出結(jié)果 ----
Hello John!
我們使用 case 表達式時:
customerName = "John"
case customerName
when "Fred" then
puts "Hello Fred!"
when "John" then
puts "Hello John!"
when "Bobert" then
puts "Hello Bob!"
else
puts "Who are you?"
end
# ---- 輸出結(jié)果 ----
Hello John!
Tips : 當沒找到匹配項,
else
中定義的默認語句會執(zhí)行。
注意事項:case 后面判斷的變量可以是任意類型的,例如:字符串、數(shù)組、數(shù)字等。
case表達式在找到結(jié)果后可以把結(jié)果返回給變量。
car = "Patriot"
manufacturer = case car
when "Focus" then "Ford"
when "Navigator" then "Lincoln"
when "Camry" then "Toyota"
when "Civic" then "Honda"
when "Patriot" then "Jeep"
when "Jetta" then "VW"
when "Ceyene" then "Porsche"
when "Outback" then "Subaru"
when "520i" then "BMW"
when "Tundra" then "Nissan"
else "Unknown"
end
puts "The " + car + " is made by " + manufacturer
# ---- 輸出結(jié)果 ----
The Patriot is made by Jeep
6. 小結(jié)
本章中我們學習了if...
語句,if...else
語句,if...elsif...
語句,三元運算符以及case表達式。