short-circuit operators - part of the spec?

P

Phil...

My book says the following about && and ||
(see below dashed line.) And I wonder if this
is built into the language or an artifact of the
compiler. Previously (in my youth) we were
taught that depending on the compiler to skip
evaluating stuff was dangerous and bad for
maintenance.
----------------------------
known as the short-circuit logical operators:
&& short-circuit AND
|| short-circuit OR
The short-circuit feature of the && operator is that it doesn't waste its
time on
pointless evaluations. A short-circuit && evaluates the left side of the
operation first
(operand one), and if operand one resolves to false, the && operator doesn't
bother
looking at the right side of the equation (operand two). The operator
already knows
that the complete expression can't possibly be true, since one operand has
already
proven to be false.
 
T

Thomas G. Marshall

Phil... said:
My book says the following about && and ||
(see below dashed line.) And I wonder if this
is built into the language or an artifact of the
compiler. Previously (in my youth) we were
taught that depending on the compiler to skip
evaluating stuff was dangerous and bad for
maintenance.

Depends upon the language, and no, there is nothing wrong with it when it is
specifically allowed.

In your youth, if you used C or C++, you have the same "short circuit"
evaluation leap that you have in java.

What /is/ a bad idea, which is probably what you're confusing this with, is
to rely upon a feature that happens to be present in a compiler but isn't
specified in the language spec. That /is/ dangerous.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top