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

L

lovecreatesbea...

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

Bartc

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?
 
M

Magic.Yang

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

lovecreatesbea...

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?

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

Thank you.
 
L

lovecreatesbea...

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;
}
 
K

Keith Thompson

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

Willem

(e-mail address 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
 
P

Peter Nilsson

Willem said:
(e-mail address 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;}

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;
 
B

Barry Schwarz

i may only require unsigned type


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
 
L

lovecreatesbea...

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.
 

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

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top