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

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

  1. Guest

    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.
    , Apr 13, 2008
    #1
    1. Advertising

  2. Bartc Guest

    "" <> wrote in message
    news:...
    > Are the following two lines equal? Suppose the expression i++ doesn't
    > overflow. They behave differently in my code.
    >



    The real C experts will come along in a minute, but let's see:

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


    This will break when p is false and i (before the increment) is true.

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


    This will break when p is false.

    What values of p and i are being used?


    --
    Bart
    Bartc, Apr 13, 2008
    #2
    1. Advertising

  3. Magic.Yang Guest

    On 4ÔÂ13ÈÕ, ÏÂÎç10ʱ30·Ö, ""
    <> 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;}
    >
    > Thank you for your time.


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

    On Apr 13, 10:57 pm, Eric Sosman <> wrote:
    > 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;}

    >
    > They are not equivalent. Consider the case i==0.


    Thank you.

    I forgot that the variable i stars with 0.
    , Apr 13, 2008
    #4
  5. Guest

    On Apr 13, 10:41 pm, "Bartc" <> wrote:
    > "" <> wrote in message
    >
    > news:...
    >
    > > Are the following two lines equal? Suppose the expression i++ doesn't
    > > overflow. They behave differently in my code.

    >
    > The real C experts will come along in a minute, but let's see:
    >
    > >    if (!p ? i++ : 0) break;

    >
    > This will break when p is false and i (before the increment) is true.
    >
    >
    >
    > >    if (!p){ i++; break;}

    >
    > This will break when p is false.
    >
    > What values of p and i are being used?


    p is a valid pointer, i may start with (int)0. I'm clear on this now.

    Thank you.
    , Apr 13, 2008
    #5
  6. Guest

    On Apr 13, 10:57 pm, Eric Sosman <> wrote:
    > 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;}

    >
    > They are not equivalent. Consider the case i==0.


    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;
    }
    , Apr 13, 2008
    #6
  7. "" <> writes:
    > 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.


    Apart from any difference in behavior, the first is ugly.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 13, 2008
    #7
  8. On Sun, 13 Apr 2008 08:33:07 -0700, wrote:
    > On Apr 13, 10:57 pm, Eric Sosman <> wrote:
    >> 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;}

    >>
    >> They are not equivalent. Consider the case i==0.

    >
    > They may be equal when using prefix increment operator.


    Then consider i == -1.

    > The first case spans two lines but the latter occupies four lines,


    As quoted here, both cases span one line.
    Harald van Dijk, Apr 13, 2008
    #8
  9. 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. Guest

    On Apr 13, 11:44 pm, Harald van D©¦k <> wrote:
    > On Sun, 13 Apr 2008 08:33:07 -0700, wrote:
    > > On Apr 13, 10:57 pm, Eric Sosman <> wrote:
    > >> 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;}

    >
    > >> They are not equivalent. Consider the case i==0.

    >
    > > They may be equal when using prefix increment operator.

    >
    > Then consider i == -1.
    >


    i may only require unsigned type

    > > The first case spans two lines but the latter occupies four lines,

    >
    > As quoted here, both cases span one line.


    I mean these two forms:

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


    /*2*/
    if (!p){
    ++i;
    break;
    }
    , Apr 13, 2008
    #10
  11. Willem wrote:
    > 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;}


    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
    Peter Nilsson, Apr 13, 2008
    #11
  12. On Sun, 13 Apr 2008 09:01:59 -0700 (PDT),
    "" <> wrote:

    >On Apr 13, 11:44 pm, Harald van D©¦k <> wrote:
    >> On Sun, 13 Apr 2008 08:33:07 -0700, wrote:
    >> > On Apr 13, 10:57 pm, Eric Sosman <> wrote:
    >> >> 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;}

    >>
    >> >> They are not equivalent. Consider the case i==0.

    >>
    >> > They may be equal when using prefix increment operator.

    >>
    >> Then consider i == -1.
    >>

    >
    >i may only require unsigned type
    >
    >> > The first case spans two lines but the latter occupies four lines,

    >>
    >> As quoted here, both cases span one line.

    >
    >I mean these two forms:
    >
    > /*1*/
    > if (!p ? ++i : 0)
    > break;
    >
    >
    > /*2*/
    > if (!p){
    > ++i;
    > break;
    > }


    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. Guest

    On Apr 14, 7:03 am, Barry Schwarz <> wrote:
    > On Sun, 13 Apr 2008 09:01:59 -0700 (PDT),
    > "" <> wrote:
    > >On Apr 13, 11:44 pm, Harald van D?|k <> wrote:
    > >> On Sun, 13 Apr 2008 08:33:07 -0700, wrote:
    > >> > On Apr 13, 10:57 pm, Eric Sosman <> wrote:
    > >> >> 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;}

    >
    > >> >> They are not equivalent. Consider the case i==0.

    >
    > >> > They may be equal when using prefix increment operator.

    >
    > >> Then consider i == -1.

    >
    > >i may only require unsigned type

    >
    > >> > The first case spans two lines but the latter occupies four lines,

    >
    > >> As quoted here, both cases span one line.

    >
    > >I mean these two forms:

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

    >
    > > /*2*/
    > > if (!p){
    > > ++i;
    > > break;
    > > }

    >
    > 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.
    >


    Thank you for sharing this knowledge and experience.
    , Apr 14, 2008
    #13
    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. Jason Coyne  Gaijin42

    Word wrap line break code and algorithm for c#

    Jason Coyne Gaijin42, Apr 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    24,179
    Jason Coyne Gaijin42
    Apr 8, 2004
  2. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl
    Replies:
    1
    Views:
    5,140
    Big and Blue
    Jan 30, 2005
  3. Jaco

    Client Side Break Mode

    Jaco, Jul 17, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    324
  4. Andrew
    Replies:
    3
    Views:
    372
    Andrew
    Aug 22, 2003
  5. Maurice Walmsley

    Re: Windows Update Break ASP.NET 1.0 on WinXP Pro

    Maurice Walmsley, Aug 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    326
    Andrew
    Aug 22, 2003
Loading...

Share This Page