Elixir Decision Making - Elixir

What is Decision Making in Elixir?

In Decision making structures programmer have to specify one or more conditions to be evaluated or tested by the program, along with a statement (s) to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Below is the general from of a typical decision making structure found in most of the programming language:

decision_making.jpg

Elixir provides if/else conditional constructs similar to other programming languages. It also has a Cond statement which calls the first true value it finds. Case is another control flow statement which uses pattern matching for controlling the flow of the program. Let's discuss them in depth.

Elixir provides below type of decision making statements.

S. No.

Statement and Description

1

if statement

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

2

if..else statement

If statement will be followed by an optional else statement (within the do..end block), which will execute only when the Boolean expression is false.

3

unless statement

Unless statement will have the same body as in if statement and code within unless statement will execute only when the condition specified is false.

4

unless..else statement

Unless..else statement will have the same body as an if..else statement and code within unless statement will execute only when the condition specified is false.

5

cond

cond statement is used where code has to be executed on basis of several conditions. It works like an if...else if….else construct in several other programming languages.

6

case

Case statement is 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 will execute the code associated with that case and exits case statement.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Elixir Topics