Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

short circuiting and parenthesis

Does it matter how I group subexpressions when dealing with a single short-circuiting operator?

a && b && c && d
a && (b && (c && d))
(a && b) && (c && d)
((a && b) && c) && d

Are the above expressions equivalent?

like image 923
foobar Avatar asked Aug 26 '11 07:08

foobar


People also ask

What is short-circuiting in behavior?

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.

What does short-circuiting mean in Boolean logic?

By short-circuiting, we mean the stoppage of execution of boolean operation if the truth value of expression has been determined already. The evaluation of expression takes place from left to right. In python, short-circuiting is supported by various boolean operators and functions.

What is the use of short-circuiting?

A short circuit (sometimes abbreviated to short or s/c) is an electrical circuit that allows a current to travel along an unintended path with no or very low electrical impedance.

What is short-circuiting in programming?

A short-circuit operator is used in some programming languages to execute a second argument when the first argument's outcome is not sufficient to evaluate the full expression. It provides users developing the program with more control over how expressions and arguments are processed.


1 Answers

Yes, those expressions are all equivalent, including their short-circuiting behaviour.

The parentheses change the order in which the individual &&s are evaluated. However, as && is always left-associative, the terms are always evaluated in a left-to-right order. So as soon as a term is found to be false, the rest can be skipped.

like image 85
Oliver Charlesworth Avatar answered Sep 28 '22 17:09

Oliver Charlesworth