PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

Réponse
 
LinkBack Outils de la discussion
Vieux 13/04/2008, 15h30   #1
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut `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.
  Réponse avec citation
Vieux 13/04/2008, 15h41   #2
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?


"lovecreatesbea...@gmail.com" <lovecreatesbeauty@gmail.com> wrote in message
news:7639486a-a09a-4c7b-a197-6774be7b7597@l28g2000prd.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



  Réponse avec citation
Vieux 13/04/2008, 15h57   #3
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

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
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 13/04/2008, 15h58   #4
Magic.Yang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

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


Yes ,It's equal,but keyword "break" is not in the block of "if"...
  Réponse avec citation
Vieux 13/04/2008, 16h04   #5
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalid> 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;}

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


Thank you.

I forgot that the variable i stars with 0.
  Réponse avec citation
Vieux 13/04/2008, 16h08   #6
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

On Apr 13, 10:41pm, "Bartc" <b...@freeuk.com> wrote:
> "lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.com> wrote in message
>
> news:7639486a-a09a-4c7b-a197-6774be7b7597@l28g2000prd.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.
  Réponse avec citation
Vieux 13/04/2008, 16h33   #7
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalid> 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;}

>
> 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;
}
  Réponse avec citation
Vieux 13/04/2008, 16h37   #8
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

"lovecreatesbea...@gmail.com" <lovecreatesbeauty@gmail.com> 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) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 13/04/2008, 16h44   #9
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

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.invalid> 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;}

>>
>> 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.
  Réponse avec citation
Vieux 13/04/2008, 16h46   #10
Willem
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

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
  Réponse avec citation
Vieux 13/04/2008, 17h01   #11
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

On Apr 13, 11:44 pm, Harald van D©¦k <true...@gmail.com> wrote:
> 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.invalid> 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;}

>
> >> 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;
}
  Réponse avec citation
Vieux 13/04/2008, 18h46   #12
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

lovecreatesbea...@gmail.com wrote:
> On Apr 13, 10:57 pm, Eric Sosman <esos...@ieee-dot-org.invalid> 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;}

>> 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
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 13/04/2008, 18h49   #13
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

lovecreatesbea...@gmail.com wrote:
> On Apr 13, 11:44 pm, Harald van D©¦k <true...@gmail.com> wrote:
>> 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.invalid> 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;}
>>>> 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
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 13/04/2008, 19h45   #14
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

Keith Thompson said:

> "lovecreatesbea...@gmail.com" <lovecreatesbeauty@gmail.com> 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,


....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
  Réponse avec citation
Vieux 13/04/2008, 23h47   #15
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

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
  Réponse avec citation
Vieux 14/04/2008, 00h03   #16
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

On Sun, 13 Apr 2008 09:01:59 -0700 (PDT),
"lovecreatesbea...@gmail.com" <lovecreatesbeauty@gmail.com> wrote:

>On Apr 13, 11:44 pm, Harald van D©¦k <true...@gmail.com> wrote:
>> 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.invalid> 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;}

>>
>> >> 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
  Réponse avec citation
Vieux 14/04/2008, 08h59   #17
lovecreatesbea...@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: `if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ?

On Apr 14, 7:03 am, Barry Schwarz <schwa...@dqel.com> wrote:
> On Sun, 13 Apr 2008 09:01:59 -0700 (PDT),
> "lovecreatesbea...@gmail.com" <lovecreatesbea...@gmail.com> wrote:
> >On Apr 13, 11:44 pm, Harald van D?|k <true...@gmail.com> wrote:
> >> 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.invalid> 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;}

>
> >> >> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,24932 seconds with 25 queries