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

Discussion in 'Java' started by Robin Wenger, Jan 19, 2011.

  1. Robin Wenger

    Robin Wenger Guest

    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
     
    Robin Wenger, Jan 19, 2011
    #1
    1. Advertising

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

    On 01/19/2011 07:54 AM, Robin Wenger wrote:
    > 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.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Jan 19, 2011
    #2
    1. Advertising

  3. Robin Wenger

    Lew Guest

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

    Joshua Cranmer wrote:
    > 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.

    --
    Lew
    Ceci n'est pas une pipe.
     
    Lew, Jan 19, 2011
    #3
  4. Re: Are concatenated conditions (in "if", "for" or "while" loop)ALWAYS completely evaluated?

    On 19-01-2011 08:49, Lew wrote:
    > 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
     
    Arne Vajhøj, Jan 20, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,275
    Tim Slattery
    Mar 30, 2005
  2. tconkling

    First statement always evaluated first?

    tconkling, Jul 9, 2005, in forum: C Programming
    Replies:
    3
    Views:
    347
    Keith Thompson
    Jul 9, 2005
  3. Uday Bidkar
    Replies:
    4
    Views:
    512
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Replies:
    17
    Views:
    602
    Seamus MacRae
    May 20, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    420
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page