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

Erlang If statement

Erlang Conditional Statements

The first decision statement we are going to look at is the 'if' statement. The following program shows the general form of this statement in Erlang.-

Syntax

if
condition ->
   statement #1;
true ->
   statement #2
end.

In Erlang, a condition is an expression whose result is true or false. If the condition is true, then execute the statement #1, otherwise execute the statement #2.

The following program isExample of a simple if expression in Erlang

Online Example

-module(helloworld). 
-export([start/0]). 
start() -> 
   A = 5, 
   B = 6, 
   
   if 
      A == B -> 
         io:fwrite("True"); 
      true -> 
         io:fwrite("False") 
   end.

The following important points should be noted about the above program:-

  • The expression used here is the comparison between variables A and B.

  • -The operator needs to be followed by an expression.

  • This ; needs to be followed by the statement #1.

  • -The operator needs to be followed by an expression of true.

  • The statement "end" must be placed here to indicate the end of the "if" block.

The output of the above program will be:

False

Erlang Conditional Statements