`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

Discussion in 'C Programming' started by lovecreatesbea..., Apr 13, 2008.

  1. Are the following two lines equal? Suppose the expression i++ doesn't
    overflow. They behave differently in my code.

    if (!p ? i++ : 0) break;

    if (!p){ i++; break;}

    Thank you for your time.
     
    lovecreatesbea..., Apr 13, 2008
    #1
    1. Advertisements

  2. lovecreatesbea...

    Bartc Guest


    The real C experts will come along in a minute, but let's see:
    This will break when p is false and i (before the increment) is true.
    This will break when p is false.

    What values of p and i are being used?
     
    Bartc, Apr 13, 2008
    #2
    1. Advertisements

  3. lovecreatesbea...

    Magic.Yang Guest

    Yes ,It's equal,but keyword "break" is not in the block of "if"...
     
    Magic.Yang, Apr 13, 2008
    #3
  4. Thank you.

    I forgot that the variable i stars with 0.
     
    lovecreatesbea..., Apr 13, 2008
    #4
  5. p is a valid pointer, i may start with (int)0. I'm clear on this now.

    Thank you.
     
    lovecreatesbea..., Apr 13, 2008
    #5
  6. They may be equal when using prefix increment operator.

    The first case spans two lines but the latter occupies four lines, is
    it suitable for me to modify code from case 2 to case 1 at most of the
    time.

    /*1*/
    if (!p ? ++i : 0)
    break;

    /*2*/
    if (!p){
    ++i;
    break;
    }
     
    lovecreatesbea..., Apr 13, 2008
    #6
  7. Apart from any difference in behavior, the first is ugly.
     
    Keith Thompson, Apr 13, 2008
    #7
  8. Then consider i == -1.
    As quoted here, both cases span one line.
     
    Harald van Dijk, Apr 13, 2008
    #8
  9. lovecreatesbea...

    Willem Guest

    wrote:
    ) Are the following two lines equal? Suppose the expression i++ doesn't
    ) overflow. They behave differently in my code.
    )
    ) if (!p ? i++ : 0) break;
    )
    ) if (!p){ i++; break;}

    Nope. This, however, should be equal to the first:

    if (!p) { if (i++) break;}


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Apr 13, 2008
    #9
  10. i may only require unsigned type
    I mean these two forms:

    /*1*/
    if (!p ? ++i : 0)
    break;


    /*2*/
    if (!p){
    ++i;
    break;
    }
     
    lovecreatesbea..., Apr 13, 2008
    #10
  11. Not if there's an 'else' attached to the first 'if', particularly if
    p == 0 and i == 0. If you include an 'else', then it's generally
    only equivalent to:

    if (!p && i++) break;
     
    Peter Nilsson, Apr 13, 2008
    #11
  12. Unless there is some horrible expense (unrelated to the language
    itself) associated with the extra lines, the obvious answer is "Go
    with the code that is easier to understand!" In the real world,
    maintenance costs usually far exceed development costs.

    Your attempt to force the code to use the conditional operator is 1)
    sufficiently obfuscated to require all these messages, and 2)
    unnecessarily and unintuitively restrictive (i cannot be 0; oops, i
    must also be unsigned).

    Since the simpler statement is the natural(tm) language construct to
    do what you want, the answer to your previous question is: NO! It is
    not suitable change case 2 into case 1.


    Remove del for email
     
    Barry Schwarz, Apr 14, 2008
    #12
  13. Thank you for sharing this knowledge and experience.
     
    lovecreatesbea..., Apr 14, 2008
    #13
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.