Posts Tagged ‘control flow’

Introduction to Erlang : Control Flow

This entry is part 8 of 16 in the series Introduction to Erlang

Control Flow

As we saw in the previous post, pattern matching with different function clauses can be used in order to control the execution flow in Erlang. Erlang also provides the if, case, and receive control flow constructs that can be used in a function body. In this post I will only present the if and casestatements since receive is used for message passing and I will write a dedicated post about the subject. Both if and case are similar to the equivalent statements of other programming languages.

if statement

The format of an if statement in Erlang is the following:

if
    Boolean_Expression1 ->
	If_body1;
    Boolean_Expression2 ->
	If_body2;
    ...
    true ->
	If_body_cath_all
end

So the different clauses, except the last one, are like else if in other languages, while the last one (true ->) is like the else; it succeeds when all the previous clauses have failed.
Read the rest of this entry »