I've come across += and -= and have no clue as to what they do, and I cant find any tutorials either. Can anyone explain?
EDIT: I've also noticed some code..
How can 'i' be a condition?Code:if (i) { etc etc etc }
I've come across += and -= and have no clue as to what they do, and I cant find any tutorials either. Can anyone explain?
EDIT: I've also noticed some code..
How can 'i' be a condition?Code:if (i) { etc etc etc }
Last edited by Sn0mAN; 07-11-2007 at 07:17 PM.
- L += R;
Equivalent to:
Code:L = L + R;- L -= R;
Equivalent to:
Code:L = L - R;
The operator != itself returns 1 or 0, so when you use
it's like you're usingCode:if(x != 0)
orCode:if( (x != 0) != 0)
ad infinitum. So you can just leave out the first != 0 and goCode:if ( (x != 0) != 0) != 0)
Every comparison operator (indeed, every operator) returns a zero or non-zero value. When you use if(x != 0), you're just using the zero or non-zero value value of x != 0. You can also use the zero or non-zero value of a variable. You could goCode:if(x)
or evenCode:int x = 1, y = x != 0; if(y)
instead ofCode:if(x)
. . . okay, so that wasn't a very good explanation. But I hope you know what I mean.Code:if(x != 0)
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
lol... Funny way of putting itor
Code:
if ( (x != 0) != 0) != 0)
ad infinitum. So you can just leave out the first != 0 and go
Code:
if(x)
I know, it's definitely a novel idea.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
With += and -= (or any of the compound assignment operators), the left-hand side is only evaluated once, instead of twice with the expanded form, so if its evaluation has a side effect, they're not equivalent (although this is uncommon).
Example:
http://books.google.com/books?id=svQ...nidOjt7qKR7yl8
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*