Erlang equivalent to if else?

The form for an if is: if -> ; -> ; ... 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 of -> ; -> ; ... 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.

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.

1 It certainly does allow to have an if without a true branch, and will throw an exception at run-time in this case. – Alexey Romanov Dec 1 '10 at 20:21 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. – Reese Moore Dec 1 '10 at 20:22 2 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 ...; X =:= 0 -> ... end.

Is more explicit and safe than using 'true' for the last clause. – I GIVE TERRIBLE ADVICE Dec 1 '10 at 20:25 True, but what if you want to test for (math syntax, not haskell) 4=20, xSure -100 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.

– I GIVE TERRIBLE ADVICE Dec 1 '10 at 20:37.

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.

– Helium3 Dec 1 '10 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'.

– I GIVE TERRIBLE ADVICE Dec 1 '10 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 – Helium3 Dec 1 '10 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. – I GIVE TERRIBLE ADVICE Dec 1 '10 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. – Helium3 Dec 1 '10 at 20:47.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions