470,848 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

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

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.
Jun 27 '08 #1
16 2034

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote in message
news:76**********************************@l28g2000 prd.googlegroups.com...
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

Jun 27 '08 #2
lovecreatesbea...@gmail.com 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.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3
On 4月13日, 下午10时30分, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
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"...
Jun 27 '08 #4
On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
lovecreatesbea...@gmail.com 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.
Jun 27 '08 #5
On Apr 13, 10:41*pm, "Bartc" <b...@freeuk.comwrote:
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote in message

news:76**********************************@l28g2000 prd.googlegroups.com...
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.
Jun 27 '08 #6
On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
lovecreatesbea...@gmail.com 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;
}
Jun 27 '08 #7
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
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) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #8
On Sun, 13 Apr 2008 08:33:07 -0700, lovecreatesbea...@gmail.com wrote:
On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>lovecreatesbea...@gmail.com 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.
Jun 27 '08 #9
lovecreatesbea...@gmail.com 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
Jun 27 '08 #10
On Apr 13, 11:44 pm, Harald van D│k <true...@gmail.comwrote:
On Sun, 13 Apr 2008 08:33:07 -0700, lovecreatesbea...@gmail.com wrote:
On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
lovecreatesbea...@gmail.com 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;
}
Jun 27 '08 #11
lovecreatesbea...@gmail.com wrote:
On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>lovecreatesbea...@gmail.com 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.
Consider the case i==-1.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #12
lovecreatesbea...@gmail.com wrote:
On Apr 13, 11:44 pm, Harald van D│k <true...@gmail.comwrote:
>On Sun, 13 Apr 2008 08:33:07 -0700, lovecreatesbea...@gmail.com wrote:
>>On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
lovecreatesbea...@gmail.com 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
Then consider i == (type_of_i)-1.
I mean these two forms:

/*1*/
if (!p ? ++i : 0)
break;
/*2*/
if (!p){
++i;
break;
}
Not equivalent. Any questions?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #13
Keith Thompson said:
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
>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,
....which we will take as read...
the first is ugly.
No difference there, then.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #14
Willem wrote:
lovecreatesbea...@gmail.com 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
Jun 27 '08 #15
On Sun, 13 Apr 2008 09:01:59 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote:
>On Apr 13, 11:44 pm, Harald van D│k <true...@gmail.comwrote:
>On Sun, 13 Apr 2008 08:33:07 -0700, lovecreatesbea...@gmail.com wrote:
On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
lovecreatesbea...@gmail.com 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
Jun 27 '08 #16
On Apr 14, 7:03 am, Barry Schwarz <schwa...@dqel.comwrote:
On Sun, 13 Apr 2008 09:01:59 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.comwrote:
On Apr 13, 11:44 pm, Harald van D?|k <true...@gmail.comwrote:
On Sun, 13 Apr 2008 08:33:07 -0700, lovecreatesbea...@gmail.com wrote:
On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
lovecreatesbea...@gmail.com 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.
Jun 27 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Martin Lucas-Smith | last post: by
6 posts views Thread by Sandman | last post: by
5 posts views Thread by Ann | last post: by
5 posts views Thread by viza | last post: by
25 posts views Thread by chunhui_true | last post: by
reply views Thread by Dominic | last post: by
8 posts views Thread by CJM | last post: by
26 posts views Thread by Alexander Korsunsky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.