Elixir Decision Making-Case statement - Elixir

What is the use of Case statement?

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. If no match is found, it exits the statement with an CaseClauseError that no matching clauses were found. You should always have a case with _ which matches all values. This will help in prevention of abovementioned error. Also this is comparable to the default case in switch-case statements.

Syntax

Example

While running above program, it produces following result:

Remember that the case selection is done using pattern matching, so you can use the standard pattern matching techniques.

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

Elixir Topics