Short-Circuit Boolean Expressions
Short-circuit boolean expressions are boolean expressions whose arguments are evaluated only when necessary.
There are two “short-circuit” boolean expressions.
-
Expr1
orelseExpr2
-
This first evaluates
Expr1
. IfExpr1
evaluates totrue
,Expr2
is not evaluated. IfExpr1
evaluates tofalse
,Expr2
is evaluated. -
Expr1
andalsoExpr2
-
This first evaluates
Expr1
. IfExpr1
evaluates totrue
,Expr2
is evaluated. IfExpr1
evaluates tofalse
,Expr2
is not evaluated.
Note: In the corresponding boolean expressions (A or B
;
A and B
), both the arguments are always evaluated,
even if the truth value of the expression can be determined by
evaluating only the first expression.
Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.