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

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

lovecreatesbea...@gmail.com
Guest
Posts: n/a

 04-13-2008
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;}

Bartc
Guest
Posts: n/a

 04-13-2008

"(E-Mail Removed)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

Magic.Yang
Guest
Posts: n/a

 04-13-2008
On 4月13日, 下午10时30分, "(E-Mail Removed)"
<(E-Mail Removed)> 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"...

lovecreatesbea...@gmail.com
Guest
Posts: n/a

 04-13-2008
On Apr 13, 10:57 pm, Eric Sosman <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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.

lovecreatesbea...@gmail.com
Guest
Posts: n/a

 04-13-2008
On Apr 13, 10:41*pm, "Bartc" <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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.

lovecreatesbea...@gmail.com
Guest
Posts: n/a

 04-13-2008
On Apr 13, 10:57 pm, Eric Sosman <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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;
}

Keith Thompson
Guest
Posts: n/a

 04-13-2008
"(E-Mail Removed)" <(E-Mail Removed)> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Harald van D某k
Guest
Posts: n/a

 04-13-2008
On Sun, 13 Apr 2008 08:33:07 -0700, (E-Mail Removed) wrote:
> On Apr 13, 10:57 pm, Eric Sosman <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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.

Willem
Guest
Posts: n/a

 04-13-2008
(E-Mail Removed) 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

lovecreatesbea...@gmail.com
Guest
Posts: n/a

 04-13-2008
On Apr 13, 11:44 pm, Harald van D│k <(E-Mail Removed)> wrote:
> On Sun, 13 Apr 2008 08:33:07 -0700, (E-Mail Removed) wrote:
> > On Apr 13, 10:57 pm, Eric Sosman <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) 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;
}