Elixir - If else 語句



if..else 語句由一個布林表示式後跟一個或多個語句組成。其後是具有一個或多個語句的 else 語句。

語法

if..else 語句的語法如下所示 −

if boolean-statement do
   #Code to be executed if condition is satisfied
else
   #Code to be executed if condition is not satisfied
end

如果布林表示式計算結果為 true,那麼 if 語句中的程式碼塊將被執行。如果布林表示式計算結果為 false,那麼將在給定 if 語句的 else 關鍵字後執行程式碼。

流程圖

If Else Statement

示例

a = false
if a === true do
   IO.puts "Variable a is true!"
else
   IO.puts "Variable a is false!"
end
IO.puts "Outside the if statement"

以上程式將生成以下結果。

Variable a is false! 
Outside the if statement
elixir_decision_making.htm
廣告
© . All rights reserved.