|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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"... |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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; } |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"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" |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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; } |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|