+ 3
In a ssignment statment the value on left of the equal sign is always equal to value of the right? Why ?
2 Réponses
+ 9
With assignment statment we assign the value of the right expreisson to the variable on the left of the '=' sign.
thus, this mean that the value of the left side is always equal to that of the right side.
+ 5
tip: to avoid misassignment when comparing values (using ==) with functions I write my if statements backwards to that of the usual convention to avoid misassignment of data because a missing =
example normal ( can be a hard to find error )
a = 5;
int SomeFunction();
if(a == SomeFunction()) {
// misassignment because of missing =
// if(a = SomeFunction())
// do magics
}
safe way ( misassignment will detected at compile)
if(SomeFunction() == a) {
// if(SomeFunction() = a)
// do magics
}
note: this method won't help when just comparing two local variables (such as two ints or two chars) as both are usually writable.
I feel it is a useful thing to do. (I have been caught out by this and it was very frustrating to find that I had simply missed an = sign in my haste). others may disagree but thems the breaks I guess.