Are concatenated conditions (in "if", "for" or "while" loop) ALWAYS completely evaluated?

R

Robin Wenger

Assume I setup a condition with multiple concatenated sub-conditions like:

for (i=0; i < othervar && myobj != null && i > myobj.getValue(); i++) { ...}

or

if (i < othervar && myobj != null && i > myobj.getValue()) { ... }

are all these sub-conditions ALWAYS evaluated?

Refering to the sample above: If i is GREATER than othervar is then still checked if myobj != null?

Robin
 
J

Joshua Cranmer

Assume I setup a condition with multiple concatenated sub-conditions like:

for (i=0; i< othervar&& myobj != null&& i> myobj.getValue(); i++) { ...}

or

if (i< othervar&& myobj != null&& i> myobj.getValue()) { ... }

are all these sub-conditions ALWAYS evaluated?

The && and || are what is known as "short-circuit operators". This means
that when the result of the operation is known, they do not execute the
rest of the expression. So, for &&, if the first expression is false,
the other expressions are not evaluated; for ||, if the first expression
is true, the other expressions are not evaluated.

This is must heavily used with, e.g.:
if (param != null && param.getFoo() == 10)


Also, if you cross-post to multiple newsgroups and only have the
follow-up set to one, PLEASE indicate that in the body of your text. Or
better yet, don't do it all--news servers and newsreaders are designed
to handle cross-posted messages properly.
 
L

Lew

Joshua said:
Also, if you cross-post [sic] to multiple newsgroups and only have the follow-up set
to one, PLEASE indicate that in the body of your text. Or better yet, don't do
it all--news servers and newsreaders are designed to handle cross-posted
messages properly.

What is this sudden flood of cross-posted messages recently?

It's not necessary, especially not in comp.lang.java.**.

It's annoying to enthusiastically answer a question only to find that the
thrust of the conversation is in a different newsgroup. It can also confuse,
when one doesn't notice the f/u and wonders why their answer hasn't shown up.
 
A

Arne Vajhøj

What is this sudden flood of cross-posted messages recently?

It's not necessary, especially not in comp.lang.java.**.

It's annoying to enthusiastically answer a question only to find that
the thrust of the conversation is in a different newsgroup. It can also
confuse, when one doesn't notice the f/u and wonders why their answer
hasn't shown up.

Its must be in fashion right now.

But I agree that it is nuisance.

Arne
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top