16 Ruby 條件語句
Ruby 提供了現代語言常見的條件結構,在本章節中我們會學習到如何在 Ruby 中所有可用的條件語句使用方式。
1. if… 語句
if...
語句是條件語句中最基本的,當 if
後的條件表示式得到滿足的時候,執行程式碼部分,不滿足條件的時候,程式碼將被忽略。
例項:
if 10 < 20 then
puts "10 is less than 20"
end
# ---- 輸出結果 ----
10 is less than 20
解釋:在執行時,程式碼將顯示字串“10 is less than 20”,因為 10 < 20
表示式的計算結果為true
。end
語句標誌著if
語句的結束。
經驗
then
,得到的結果是相同的。
例項:
if 10 < 20
puts "10 is less than 20"
end
# ---- 輸出結果 ----
10 is less than 20
另外,我們還可以將 if
語句的判斷部分後置,這叫做條件後置:
例項:
puts "10 is less than 20" if 10 < 20
# ---- 輸出結果 ----
10 is less than 20
if
後面的條件表示式可以使用邏輯運算子。
例項:
firstname = 'john'
lastname = "smith"
if firstname == "john" && lastname == "smith"
puts "Hello John!"
end
# ---- 輸出結果 ----
Hello John!
還有一種跟 if
相反的判斷,名叫 unless
,它和 if
剛好相反,當 unless
後面的條件不滿足的時候,才執行程式碼部分。
例項:
unless 10 >= 20
puts "10 not bigger than or equal 20"
end
# ---- 輸出結果 ----
10 not bigger than or equal 20
注意事項:unless
也可以稱為 if not,因為很容易讓人混淆,所以我們儘量不使用它。
2. if…else語句
上面的 if
表示式只提供了條件表示式計算結果為 true
,的情況,並沒有為結果為 false
時進行考慮,if...else
語句在這時就起到了作用。
例項:
customerName = "Andrew"
if customerName == "Fred"
puts "Hello Fred!"
else
puts "You're not Fred! Where's Fred?"
end
# ---- 輸出結果 ----
You're not Fred! Where's Fred?
解釋:當條件表示式為true
的時候,執行else
之前的內容,當表示式為false
的時候,執行else
之後的內容。
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
# ---- 輸出結果 ----
Hello Andrew!
我們也可以在某些情況省略 else
。
例項:
customerName = "Andrew"
if customerName == "Fred"
puts "Hello Fred!"
elsif customerName == 'Andrew'
puts "Hello Andrew!"
end
# ---- 輸出結果 ----
Hello Andrew!
4. 三元運算子
三元運算子是一種便捷表達條件語句的形式,它的語法如下:
[條件] ? [true expression] : [false expression]
當條件為true
的時候,會執行冒號左側代表真的表示式,當條件為false
的時候,會執行冒號右側代表假的表示式。
例項:
customerName = "Andrew"
puts customerName == "Fred" ? "Hello Fred" : "Who are you?"
# ---- 輸出結果 ----
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
# ---- 輸出結果 ----
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
# ---- 輸出結果 ----
Hello John!
Tips : 當沒找到匹配項,
else
中定義的預設語句會執行。
注意事項:case 後面判斷的變數可以是任意型別的,例如:字串、陣列、數字等。
case表示式在找到結果後可以把結果返回給變數。
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
# ---- 輸出結果 ----
The Patriot is made by Jeep
6. 小結
本章中我們學習了if...
語句,if...else
語句,if...elsif...
語句,三元運算子以及case表示式。