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

Erlang Multiple Expressions

Conditional Statements in Erlang

The if expression also allows the simultaneous calculation of multiple expressions. The general form of this statement in Erlang is shown in the following program−

Syntax

if
condition1 ->
   statement#1;
condition2 ->
   statement#2;
conditionN ->
   statement#N;
true ->
   defaultstatement
end.

In Erlang, a condition is an expression that evaluates to true or false. If the condition is true, then statement # will be executed.1Otherwise, the next condition will be calculated in sequence. If no calculation result is true, then defaultstatement will be calculated.

The following diagram is a general illustration of the given statement above.

The following program isAn example of a simple if expression in Erlang-

Online Example

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

For the above program, the following key points should be noted-

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

  • -The > operator needs to follow an expression.

  • Will;Needs to follow the statement #1.

  • -The > operator needs to follow an expression of true

  • The statement 'end' needs to indicate the end of the if block here.

The output of the above program will be-

Output Result

A is less than B

Conditional Statements in Erlang