syntax question
-
Hi. Can anyone explain to me once and for all why do i see people write something like: if (0==myVar) { ... } instead of: if (myVar==0) { ... } what difference does it make ?
-
Hi. Can anyone explain to me once and for all why do i see people write something like: if (0==myVar) { ... } instead of: if (myVar==0) { ... } what difference does it make ?
Simple. Your compiler will tell you if you type if (0 = myVar) whereas if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic
-
Simple. Your compiler will tell you if you type if (0 = myVar) whereas if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic
Christian Graus wrote: if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. A typo, I guess Christian... :) The value of the expression "A = B" is the value of A after the assignment, i.e. the value of B. Thus, "myVar = 0" evaluates to 0 and false. -- Human beings, who are almost unique in having the ability
to learn from the experience of others, are also remarkable
for their apparent disinclination to do so. (Douglas Adams) -
Christian Graus wrote: if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. A typo, I guess Christian... :) The value of the expression "A = B" is the value of A after the assignment, i.e. the value of B. Thus, "myVar = 0" evaluates to 0 and false. -- Human beings, who are almost unique in having the ability
to learn from the experience of others, are also remarkable
for their apparent disinclination to do so. (Douglas Adams)both of you got it wrong... i asked what is the difference between: if (a==0) { } and if (0==a) { } and not between: if (a=0) { } and if (0=a) { } (notice the extra = sign which makes a big difference)
-
Simple. Your compiler will tell you if you type if (0 = myVar) whereas if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic
checkout my reply to Johann and try to re-answer. Thanks.
-
checkout my reply to Johann and try to re-answer. Thanks.
They did answer. If you get in the habit of always typing the constant first, the compiler will emit an error message when you mistakenly type one equals ('=') instead of two ('=='). Other than that, the only difference between (a == b) and (b == a) is the order of evaluation.
Software Zen:
delete this;
-
They did answer. If you get in the habit of always typing the constant first, the compiler will emit an error message when you mistakenly type one equals ('=') instead of two ('=='). Other than that, the only difference between (a == b) and (b == a) is the order of evaluation.
Software Zen:
delete this;
Thanks. I was under the impression that it had somthing to do with compiler or preprocessor optimizations...
-
They did answer. If you get in the habit of always typing the constant first, the compiler will emit an error message when you mistakenly type one equals ('=') instead of two ('=='). Other than that, the only difference between (a == b) and (b == a) is the order of evaluation.
Software Zen:
delete this;
-
both of you got it wrong... i asked what is the difference between: if (a==0) { } and if (0==a) { } and not between: if (a=0) { } and if (0=a) { } (notice the extra = sign which makes a big difference)
Lior Shoval wrote: both of you got it wrong... i asked what is the difference between: if (a==0) { } and if (0==a) { } Christian did answer that one (although he rationalized a couple of '"''s off :)). I merely made it clearer what a = 0 evaluates to. -- Human beings, who are almost unique in having the ability
to learn from the experience of others, are also remarkable
for their apparent disinclination to do so. (Douglas Adams) -
Simple. Your compiler will tell you if you type if (0 = myVar) whereas if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic
Unless, of course, you use warning level 4, which will give the compiler warning "warning C4706: assignment within conditional expression" if you try to do
if (myVar = 0)
. This happened only in VC++ 5 or 6, I believe; before that, you had to do theif (0 = myVar)
hack to get the compiler to complain. Best wishes, Hans -
That means, that using the first method is better to catch typos like "=" that act like assignments? regards
Yes. The people who use that method like it for that reason. I don't use the method myself, since it reduces the readability of the conditional expression. For example, the expressions
(x == 7)
and(7 == x)
evaluate the same and produce the same result. If you're reading the code, however, the first expression is read as "ifx
is equal to7
" and the second expression is read as "if7
is equal tox
". The reading of the second expression seems awkward to me. I prefer to think of testing a variable against a constant.
Software Zen:
delete this;
-
Yes. The people who use that method like it for that reason. I don't use the method myself, since it reduces the readability of the conditional expression. For example, the expressions
(x == 7)
and(7 == x)
evaluate the same and produce the same result. If you're reading the code, however, the first expression is read as "ifx
is equal to7
" and the second expression is read as "if7
is equal tox
". The reading of the second expression seems awkward to me. I prefer to think of testing a variable against a constant.
Software Zen:
delete this;
-
Yes. The people who use that method like it for that reason. I don't use the method myself, since it reduces the readability of the conditional expression. For example, the expressions
(x == 7)
and(7 == x)
evaluate the same and produce the same result. If you're reading the code, however, the first expression is read as "ifx
is equal to7
" and the second expression is read as "if7
is equal tox
". The reading of the second expression seems awkward to me. I prefer to think of testing a variable against a constant.
Software Zen:
delete this;
I have the same problem. It just feels more natural, and easer to read/write (x==7) instead of (7==x). But good habits (a.k.a constant==variable) are good habits. I keep trying to break this one, but habits are haibts.:) Trust in the code Luke. Yea right!