Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: equality op...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 45 Topic 45751 of 47034
Post > Topic >>

Re: equality operator question

by "Jim Langston" <tazmaster@[EMAIL PROTECTED] > May 5, 2008 at 02:51 AM

Jack Klein wrote:
> On Sun, 4 May 2008 22:40:28 -0400, "barcaroller"
> <barcaroller@[EMAIL PROTECTED]
> wrote in comp.lang.c++:
>
>> Is it now common practice to use conditional statements like
>>
>>     if (0    == i)
>>     if (-1   == i)
>>     if (true == i)
>>     if (5    == i)
>>
>> over the more traditional statements like
>>
>>     if (i ==    0)
>>     if (i ==   -1)
>>     if (i == true)
>>     if (i ==    5)
>>
>> for all types of 'i' (PODs and cl*****)?
>
> The reason for writing the constant first in an equality comparison is
> quite simple.  A fairly common typographical error is type only one
> '=' once in a while when two are intended, and needed.
>
> Consider the implications of accidentally typing:
>
>   if (i = 0)
>
> ...when you meant to type:
>
>   if (i == 0)
>
> Assuming a built-in type, or even a class type having an assignment
> operator accepting an int, then the first version, the type, is not
> only always false, but it also destroys the existing value of 'i'. Yet
> it is syntactically correct and swallowed by the compiler without
> complaint.
>
> On the other hand, if you accidentally type "if (0 = i), the compiler
> will emit a diagnostic and reject it.
>
> So it is a very inexpensive method of catching a typing error on the
> very first compile.  This is not an extremely common error, but is one
> which happens to everyone once in a while.  And of course it doesn't
> help when you are comparing two objects, but comparisons with a
> constant are fairly frequent.
>
> Some programmers consider it hideously ugly and refuse to ever use it.
> I think it is useful and I use it.  It works, and it saves time when
> you do happen to make that typo.

I agree, but my compiler warns me if an assignment is being made inside an

if statement.  Otherwise I probably would use that format.  But since it 
does, I stick with the more traditional if ( variable == value )

-- 
Jim Langston
tazmaster@[EMAIL PROTECTED]

 




 45 Posts in Topic:
equality operator question
"barcaroller" &  2008-05-04 22:40:28 
Re: equality operator question
Jack Klein <jackklein@  2008-05-04 21:51:54 
Re: equality operator question
"Jim Langston"   2008-05-05 02:51:12 
Re: equality operator question
"kwikius" <a  2008-05-05 11:58:28 
Re: equality operator question
James Kanze <james.kan  2008-05-05 06:19:44 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-05 15:28:16 
Re: equality operator question
James Kanze <james.kan  2008-05-05 07:12:07 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 07:48:06 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-05 17:07:23 
Re: equality operator question
Eberhard Schefold <eba  2008-05-06 08:02:24 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:53:14 
Re: equality operator question
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-06 16:08:43 
Re: equality operator question
"kwikius" <a  2008-05-06 17:32:37 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 13:39:06 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 19:46:31 
Re: equality operator question
"Bo Persson" &l  2008-05-06 20:00:21 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 15:18:44 
Re: equality operator question
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-06 18:17:40 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-07 22:02:04 
Re: equality operator question
Jerry Coffin <jcoffin@  2008-05-07 22:38:59 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-05 12:32:00 
Re: equality operator question
peter koch <peter.koch  2008-05-05 11:08:13 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 15:33:29 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-05 20:36:41 
Re: equality operator question
"barcaroller" &  2008-05-05 23:01:32 
Re: equality operator question
Christian Hackl <hacki  2008-05-06 09:34:51 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:49:33 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 23:58:01 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:52:13 
Re: equality operator question
James Kanze <james.kan  2008-05-06 03:04:22 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:04:19 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-06 16:23:15 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:35:26 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-06 14:48:53 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 17:13:01 
Re: equality operator question
James Kanze <james.kan  2008-05-06 03:09:18 
Re: equality operator question
James Kanze <james.kan  2008-05-06 05:06:53 
Re: equality operator question
Nick Keighley <nick_ke  2008-05-06 09:06:24 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-06 11:50:27 
Re: equality operator question
James Kanze <james.kan  2008-05-07 01:53:25 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-07 15:16:59 
Re: equality operator question
James Kanze <james.kan  2008-05-09 04:34:21 
Re: equality operator question
Lionel B <me@[EMAIL PR  2008-05-06 14:31:59 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:36:25 
Re: equality operator question
Lionel B <me@[EMAIL PR  2008-05-07 09:08:00 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:05:06 CDT 2008.