Elixir - Unless else 語句



一個 unless..else 語句由一個布林表示式以及一個或多個語句組成。它後面還跟一個 else 語句,及其自身的語句塊。

語法

下面是 unless..else 語句的語法 −

unless boolean-statement do
   #Code to be executed if condition is false
else
   #Code to be executed if condition is true
end

如果布林表示式評估為 false,則 unless 語句中的程式碼塊將被執行。如果布林表示式評估為 true,則 given unless 語句的 else 關鍵字之後的程式碼將被執行。

a = false
unless a === false do
   IO.puts "Condition is not satisfied"
else
   IO.puts "Condition was satisfied!"
end
IO.puts "Outside the unless statement"

上面的程式生成了以下結果。

Condition was satisfied!
Outside the unless statement
elixir_decision_making.htm
廣告
© . All rights reserved.