当前位置: 首页 > 文档资料 > Elixir 中文教程 >

if..else statement

优质
小牛编辑
121浏览
2023-12-01

if..else语句由一个布尔表达式后跟一个或多个语句组成。 接下来是带有一个或多个语句的else语句。

语法 (Syntax)

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关键字之后的代码。

流程图 (Flow Diagram)

如果是其他声明

例子 (Example)

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