Ruby 條件語句
Ruby 提供了現(xiàn)代語言常見的條件結(jié)構(gòu),在本章節(jié)中我們會學(xué)習(xí)到如何在 Ruby 中所有可用的條件語句使用方式。
1. if… 語句
if...
語句是條件語句中最基本的,當(dāng) if
后的條件表達(dá)式得到滿足的時候,執(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
表達(dá)式的計算結(jié)果為true
。end
語句標(biāo)志著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
后面的條件表達(dá)式可以使用邏輯運算符。
實例:
firstname = 'john'
lastname = "smith"
if firstname == "john" && lastname == "smith"
puts "Hello John!"
end
# ---- 輸出結(jié)果 ----
Hello John!
還有一種跟 if
相反的判斷,名叫 unless
,它和 if
剛好相反,當(dāng) 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
表達(dá)式只提供了條件表達(dá)式計算結(jié)果為 true
,的情況,并沒有為結(jié)果為 false
時進(jìn)行考慮,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?
解釋:當(dāng)條件表達(dá)式為true
的時候,執(zhí)行else
之前的內(nèi)容,當(dāng)表達(dá)式為false
的時候,執(zhí)行else
之后的內(nèi)容。
3. if…elsif…語句
當(dāng)有許多條件需要判斷的時候需要使用 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. 三元運算符
三元運算符是一種便捷表達(dá)條件語句的形式,它的語法如下:
[條件] ? [true expression] : [false expression]
當(dāng)條件為true
的時候,會執(zhí)行冒號左側(cè)代表真的表達(dá)式,當(dāng)條件為false
的時候,會執(zhí)行冒號右側(cè)代表假的表達(dá)式。
實例:
customerName = "Andrew"
puts customerName == "Fred" ? "Hello Fred" : "Who are you?"
# ---- 輸出結(jié)果 ----
Who are you?
5. case 表達(dá)式
當(dāng)我們多次使用if...elsif...
來檢查值的時候,可以使用 case 表達(dá)式來替代。
實例:
比如使用 if..elsif...
時表達(dá)式為這個亞子:
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 表達(dá)式時:
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 : 當(dāng)沒找到匹配項,
else
中定義的默認(rèn)語句會執(zhí)行。
注意事項:case 后面判斷的變量可以是任意類型的,例如:字符串、數(shù)組、數(shù)字等。
case表達(dá)式在找到結(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é)
本章中我們學(xué)習(xí)了if...
語句,if...else
語句,if...elsif...
語句,三元運算符以及case表達(dá)式。