-1
$\begingroup$

I would like to implement short circuit evaluation logic in my code. And I want to know about the full details how it works?

Ex:

function a() {return true;}

function b() {return false;}

function c() {return true;}

Expression

Case 1) a() && b() && c();

Case 2) a() || b() && c();

Case 3) a() && c() || b();

Case 1:

b() will not be executed.

Case 2:

b() and c() will not be executed

Case 3:

c() will not be executed.

Where should we learn about this short-circuit evaluation?

$\endgroup$
4
  • 1
    $\begingroup$ Have you tried submitting your question to a web search engine. You would have found the answer with wikipedia. You are supposed to do a little bit of effort on your own before asking. So what have you tried? Where did you look? $\endgroup$ – babou May 1 '15 at 10:20
  • $\begingroup$ I read wikipedia and some other websites too. It is not fully covered. $\endgroup$ – kannanrbk May 1 '15 at 10:23
  • $\begingroup$ OK, then it helps us to know what you did look at, and to give some words as to what remains a problem, or what seems wrong or incomplete in what you read. $\endgroup$ – babou May 1 '15 at 10:30
  • $\begingroup$ Please edit your question to ask a more narrowly focused question. If there's something specific that's not covered in Wikipedia, ask about that. Right now your question is just "tell me everything you know about short-circuit evaluation", which is too broad. We want you to pose a specific, answerable technical question. We also want you to tell us what research you've done, to help us give you a more relevant answer. See cs.stackexchange.com/help/how-to-ask $\endgroup$ – D.W. May 1 '15 at 18:36
4
$\begingroup$

Short-circuit evaluation is based on lazy-evaluation (call-by-need evaluation) of the arguments of some function or operator. This is all described in various wikipedia articles.

The idea is that the function call defers requesting evaluation of its arguments until they are actually needed, possibly never evaluating them if the computation may proceed independently of their value.

The expression "short-circuit evaluation" has been used mostly, at least initially, for boolean operations. For example, a conjunction (&&) will always return false, if one of the arguments is false. So if the first argument evalates to false, there is no need to evaluate the second.

But it is is true, and the second is false, you would think that it was not necessary to evaluate the first. This is correct, but there is not much you can do about it. You have to start with one of them, and the order is usually taken from left to right. So you may start with one that was not necessary.

Thus short-circuit evaluation also has the implicit idea that there is an order of evaluation, and that an argument is not evaluated if previously evaluated arguments show it is not needed.

Similarly, for a disjunction (||), if the first argument evaluates to true, there is no need to know the second argument to know the end result: true.

The if ... then ... else ... conditional, seen as an operator (I am skipping details) is always evaluated in short-circuit, skipping the second or third argument evaluation depending on the value of the first.

The concept can be extended to arithmetics, for example by not evaluating $b()$ in $a()\times b()$ when $a()$ evaluates to $0$.

Short-circuit evaluation does not change the end-result of a program, under the consition that evaluation of arguments has no side-effect. However, it may save on computation time, to such an extent that some programs terminate with short-circuit evaluation, that would not terminate without it. This is typically the case if an argument that is ignored would otherwise cause an infinite computation (there is also the case where it would cause a run-time error, but this may be considered a side-effect).

In your example (assuming as usual left associativity), your answers are incorrect.

For example, considering case 1: b() is evaluated, because a() returns true as first argument of a conjunction. But c() is not evaluated because b() returns false, so that a()&&b() returns false. The end result is false.

I am sure you can work out correct answers for the other two cases (currently wrong in the question).

$\endgroup$

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