15

I have 2 parts of code I want to execute. Both are conditionals

if Value1 < N do something 

else if Value1 >= N do something

if Value2 < N do something 

else if Value2 >= N do something

I want at one statement of each to execute.

How does the if work in erlang? there is no else. I use multiple guards, but that looks like I have 4 if statements. in groups of 2.

if some condition   
code;

if other condition  
code

end.

I get a syntax error.

2
  • I have found that if you are relying on guards or case statements, you are probably "doing it wrong" most of the time in Erlang. You probably should be doing pattern matching most of the time in Erlang as an alternative to guards and case statements. Without a concrete example, we can't tell you if you should be doing a case, guard or pattern matching, but pattern matching is always write compared to the alternatives.
    – user177800
    Dec 3, 2010 at 18:30
  • possible duplicate of How to implement if-else Sep 4, 2013 at 16:10

4 Answers 4

23

The form for an if is:

if
    <guard 1> -> <body1> ;
    <guard 2> -> <body2> ;
    ...
end

It works trying the guards in if-clauses in top-down order (this is defined) until it reaches a test which succeeds, then the body of that clause is evaluated and the if expression returns the value of the last expression in the body. So the else bit in other languages is baked into it. If none of the guards succeeds then an if_clause error is generated. A common catch-all guard is just true which always succeeds, but a catch-all can be anything which is true.

The form for a case is:

case <expr> of
    <pat 1> -> <body1> ;
    <pat 2> -> <body2> ;
    ...
end

It works by first evaluating and then trying to match that value with patterns in the case-clauses in op-down order (this is defined) until one matches, then the body of that clause is evaluated and the case expression returns the value last expression in the body. If no pattern matches then a case_clause error is generated.

Note that if and case are both expressions (everything is an expression) so they both must return values. That is one reason why there is no default value if nothing succeeds/matches. Also to force you to cover all options; this is especially important for case. if is just a degenerate case of case so it inherited it. There is a bit of history of if in the Erlang Rationale which you can find on trapexit.org under user contributions.

1
  • How do I find the reference "There is a bit of history of if in the Erlang Rationale which you can find on trapexit.org under user contributions."?
    – user502187
    Mar 29, 2019 at 23:56
13

Erlang doesn't allow you to have an if without a true statement option. Whether or not this is something that is a true statement or an actual true is up to you, but it is commonplace to have your true be the else in other languages.

if 
    some_condition -> some_code;
    some_other_condition -> some_other_code;
    true -> else_code
end.

See the "What the If?" section on this page for more on this.

5
  • 4
    It certainly does allow to have an if without a true branch, and will throw an exception at run-time in this case. Dec 1, 2010 at 20:21
  • 1
    Hence, "erlang doesn't allow toy to have an if without a true statement option.". If nothing evaluates to true you are going to get an exception. Dec 1, 2010 at 20:22
  • 6
    But getting an exception IS good. It means one case didn't go through what you planned. It could be argued that catching all 'else' clauses is as bad as catching all exceptions blindly: if X > 0 -> ...; X < 0 -> ...; X =:= 0 -> ... end. Is more explicit and safe than using 'true' for the last clause. Dec 1, 2010 at 20:25
  • True, but what if you want to test for (math syntax, not haskell) 4<x<7, 1<x<2, -10<x<-7, x>=20, x<-100 to do specific things in each case, and in any other case do something else (a contrived example, maybe). Sure -100 <= x <= -10 or -7 <= x <= 1 or 2 <= x <= 4 or 7 <= x < 20 might be more explicit, but a true (or an else in another language) is faster to write and less error prone. Dec 1, 2010 at 20:34
  • 2
    It is less error prone when writing it (in appearance), not when using it. If you did make a mistake in your clauses, the error will be hidden until your data is corrupt somewhere else. Having it explicit takes longer, but is generally safer and easier to understand when reading it later on. See the link you posted, near the bottom, the quote from Richard O'Keefe. Dec 1, 2010 at 20:37
6

Remember if in Erlang has a value to return, and it's an expression. It's not that if like in C or Java.

If you want to do something for a value, the code should be something like this;

if
  % do something and get the value
  X >= Val -> Something;
  % for doing something otherwise and get the value
  true -> Else_than_the_Something 
end.

See Section for the if expression of Erlang Reference Manual for the further details.

5

First of all, I recommend you to get used to use 'case' statement, because of 'if' conditions restricted to guard expressions:

case custom_call(A) of
  1 -> do1(A);
  2 -> do2(A)
end.

There is one more way to do conditional execution besides 'if' and 'case' that works starting from R13:

  1> N =10.
  10
  2> ((N > 10) andalso more).      
  false
  3> ((N == 10) andalso equals).
  equals
6
  • If I use a case, wont it exit when it matches to some case? Or will it execute that case and move on to try match the second?
    – some_id
    Dec 1, 2010 at 20:27
  • There is no reason to blindly use case ... of ... end instead of if. If your check can be limited to guards, use 'if'. Otherwise you're uselessly complicating your code with empty match clauses in a 'case'. Dec 1, 2010 at 20:27
  • Ok, But my question remains. Will the code exit the if statements if it executes one? Or will it execute the statement and move onto the next guard? I need some code to execute depending on the value. and then I have another method which should do the same depending on a another value. First if is Value >= queue:len and the else should be Value < queue:len . So two if else statements.
    – some_id
    Dec 1, 2010 at 20:33
  • 1
    With an if or case, it will match the first one, execute it and then leave. Only if it doesn't match the first branch will it try the second, third, etc. This is not like a 'case' or 'switch' with fall-through clauses as in C. Dec 1, 2010 at 20:40
  • Ok thanks. So it will work to have the first "if else" in a case and then the next "If else" in a second case. I need the one to finish by executing one of the 2 and then move on to the next part and execute one of the 2.
    – some_id
    Dec 1, 2010 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.