Elixir - if 語句



if 語句由一個布林表示式後接一個或多個語句組成。

語法

if 語句的語法如下 −

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

如果布林表示式求值為真,則 if 語句內的程式碼塊將被執行。如果布林表示式求值為假,則會執行給定 if 語句結尾關鍵字後的第一組程式碼。

流程圖

If Statement

示例

a = true
if a === true do
   IO.puts "Variable a is true!"
   IO.puts "So this code block is executed"
end
IO.puts "Outside the if statement"

上面的程式將生成以下結果 −

Variable a is true!
So this code block is executed
Outside the if statement
elixir_decision_making.htm
廣告
© . All rights reserved.