473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

`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 2167

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin Lucas-Smith | last post by:
Is there any need to keep the final break in a switch which uses a default at the end? I.e: switch ($data) { case 'foo': # Action break;
6
by: Sandman | last post by:
Ok, so I have PHP set upp to ato_prepend and auto_append files to every script I run. So if I someone surfs to /index.php, these scripts run: init.php -> set up DB connections and...
5
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
5
by: viza | last post by:
Hi! Suppose I have int i,j,k; for(i=0;i<I;++i){ /* loop 1 */ for(j=0;j<J;++j){ /* loop 2 */ for(k=0;k<K;++k){ /* loop 3 */ if(test){
25
by: chunhui_true | last post by:
In <<expert c>>I know the break in if wich is scoped in switch is break the switch,like: switch c case 1: if(b){ break; } ...... But like this: while(a){
0
by: Dominic | last post by:
I want to create a control inheriting from Repeater, and add a property PageBreakCount. What the is the best way to render a page break: <div style="PAGE-BREAK-BEFORE:always"></div> ...at every...
3
by: Barbara Alderton | last post by:
I recently wrote an ASP.NET app that used user controls. I produced a printer-friendly output form. One requirement was a pagebreak between one part of the form and another. I had no problem...
8
by: CJM | last post by:
What is the best way to force a page break when printing from a browser? A page in my application generates a series of tables which are usually less than will fit on a page of A4. Ideally I want...
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.