English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang Case Statement

Erlang Conditional Statements

Erlang provides the case statement, which can be used to execute expressions based on the output of the case statement.

The general form of this statement is:

Syntax

case expression of
   value1 -> statement#1;
   value2 -> statement#2;
   valueN -> statement#N
end.

The general operation of this statement is as follows-

  • The expression to be calculated is placed inside the case statement. This is usually calculated to a value that will be used in the subsequent statements.

  • Each value is evaluated based on the value passed to the case expression. The subsequent statements are executed based on which value is true.

The diagram below shows the flow of the case statement.

The following program is an example of the case statement in Erlang-

Online Example

-module(helloworld). 
-export([start/0]). 
start() -> 
   A = 5,
   case A of 
      5 -> io:fwrite("The value of A is 5"); 
      6 -> io:fwrite("The value of A is 6") 
   end.

The output of the above code will be

The value of A is 5.

Erlang Conditional Statements