Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > comparison

Reply
Thread Tools

comparison

 
 
Ajay
Guest
Posts: n/a
 
      07-19-2006
What is the difference betweem

if (x == 1)

AND

if (1==x)

-Ajay


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-19-2006
Ajay said:

> What is the difference betweem
>
> if (x == 1)
>
> AND
>
> if (1==x)


The reason some people (including myself) prefer the second is that the
error of mistyping = instead of == will be picked up by the compiler.
Consider:

if(x = 1)

and

if(1 = x)

The former is legal C, but not what you wanted (or you would not have
bothered with the if). The second is not legal C, so the compiler will
diagnose it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      07-19-2006
Ajay wrote:

> What is the difference betweem
>
> if (x == 1)
>
> AND
>
> if (1==x)


The operands to `==` are in opposite orders.

Some people feel that the second form gives some immunity to
the `x = 1` problem.

Other people feel that the second order is sufficiently unnatural
that it will /cause/ problems.

It's likely that the best tactic is to pick one of the forms,
and use that consistently though your workgroup or compatibly
with your audience.

You are invited to guess my opinion from the subtle hints in
this post.

--
Chris "subtle, like a sledgehammer" Dollin
A rock is not a fact. A rock is a rock.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      07-19-2006
"Ajay" <(E-Mail Removed)> wrote:

> What is the difference betweem
>
> if (x == 1)
>
> AND
>
> if (1==x)


The latter will cause you to think that you'll be automatically warned
if you accidentally type = instead of ==, which means that you'll have a
much harder time finding the bug when you inevitably write if (y=x).

Richard
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      07-19-2006
Ajay wrote:
> What is the difference betweem
>
> if (x == 1)
>
> AND
>
> if (1==x)


Apart from typography, none.
Since my compiler will warn me about
if (x = 1)
and my normal natural language way of expressing the comparison is
if x is equal to 1
I prefer the first.

People who have turned off their diagnostics (a bad thing), or use a
less helpful compiler, or who think of 1 as a variable, as in
if 1 is equal to x,
will prefer the second.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-19-2006
"Ajay" <(E-Mail Removed)> writes:
> What is the difference betweem
>
> if (x == 1)
>
> AND
>
> if (1==x)


One is ugly. The other is not. (There's no semantic difference.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-19-2006
Richard Bos said:

> "Ajay" <(E-Mail Removed)> wrote:
>
>> What is the difference betweem
>>
>> if (x == 1)
>>
>> AND
>>
>> if (1==x)

>
> The latter will cause you to think that you'll be automatically warned
> if you accidentally type = instead of ==,


Wrong. It will cause you to *know* that you'll be automatically warned if
you accidentally type = instead of == when comparing a constant and an
lvalue.

> which means that you'll have a
> much harder time finding the bug when you inevitably write if (y=x).


No. I agree that such a bug is *hard* to find if your compiler doesn't
notice it, but just because a related error is *easier* to find, that does
not make /this/ error *harder* to find than it was before.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Desktop Graphics Card Comparison Guide Rev. 9.2 Posted! Silverstrand Front Page News 0 08-22-2005 04:08 PM
Good comparison of IE to FireFox security record Tom Betz Firefox 0 05-13-2005 04:54 PM
.Net Comparison LV ASP .Net 4 08-31-2003 09:15 PM



Advertisments