📜  Elixir-决策

📅  最后修改于: 2020-11-04 08:22:57             🧑  作者: Mango


 

决策结构要求程序员指定一个或多个要由程序评估或测试的条件,以及确定该条件为true时要执行的一个或多个语句,以及指定该条件时要执行的其他语句(可选)确定为

以下是大多数编程语言中常见的典型决策结构的概况-

做决定

Elixir像许多其他编程语言一样提供if / else条件构造。它还有一个cond语句,该语句调用找到的第一个真值。 Case是另一个控制流语句,它使用模式匹配来控制程序流。让我们深入了解它们。

Elixir提供以下类型的决策声明。单击以下链接以查看其详细信息。

Sr.No. Statement & Description
1 if statement

An if statement consists of a Boolean expression followed by do, one or more executable statements and finally an end keyword. Code in if statement executes only if Boolean condition evaluates to true.

2 if..else statement

An if statement can be followed by an optional else statement(within the do..end block), which executes when the Boolean expression is false.

3 unless statement

An unless statement has the same body as an if statement. The code within unless statement executes only when the condition specified is false.

4 unless..else statement

An unless..else statement has the same body as an if..else statement. The code within unless statement executes only when the condition specified is false.

5 cond

A cond statement is used where we want to execute code on basis of several conditions. It kind of works like an if…else if….else construct in several other programming languages.

6 case

Case statement can be considered as a replacement for switch statement in imperative languages. Case takes a variable/literal and applies pattern matching to it with different cases. If any case matches, Elixir executes code associated with that case and exits case statement.