Just confirming, is comparison 1 evaluating to comparison 2 or 3?
Code:int a = 0, b = 0, c = 0, d = 0;
//Comparison 1
if(a == b == c)
{
d++;
}
//Comparison 2
bool res = a == b;
if(res == c)
{
d++;
}
//Comparison 3
if(a == b &&
b == c)
{
d++;
}
Printable View
Just confirming, is comparison 1 evaluating to comparison 2 or 3?
Code:int a = 0, b = 0, c = 0, d = 0;
//Comparison 1
if(a == b == c)
{
d++;
}
//Comparison 2
bool res = a == b;
if(res == c)
{
d++;
}
//Comparison 3
if(a == b &&
b == c)
{
d++;
}
First off, comparisons don't evaluate to one another, they evaluate to a bool result. So I suppose you're simply asking whether they all have the same result.
In #1 I'm not 100% sure about the evaluation sequence mandated by the standard, but that doesn't matter either: In the end you're comparing the result of an equality comparison between two equal ints (i.e. true) with a third int that's 0. Since true is converted to a non-zero integer (usually 1, but I don't think that's mandatory, but any non-zero value would lead to the same result) the final result is true.
In #2 you're enforcing a certain evaluation sequence by assigning an intermediate result to a temporary variable, but aside from this it essentially is the same as #1.
#3 is obvious, isn't it? ;) You're comparing 0 and 0 for equality twice. The comparison evaluates to true in both cases, so the && eventually will evaluate to true as well.
However, I don't see what that has to do with Win API. Could it be that you were one off when clicking the section you wanted to post that in? ;)
It said, "C++ and WinAPI", so I'm taking it as C++ (since there's no C section here). If it's a wrong section, I'll let moderator move it.
Going back to my question. I'm not asking about all three situations. I'm asking about comparison #1 and what it evaluates to. And no, your assumption is not correct. The first if statement will result in a false.
C++ precedence rules aren't Windows API issues.
http://www.cppreference.com/wiki/lan...tor_precedence
a == b is true. ThenQuote:
The first if statement will result in a false.
is false, since c is 0, so the entire expression is false.Code:true == c
Regards,
Paul McKenzie
I'm trying to save myself coding time. If you look at the mathematical sense, a == b == c should evaluate to "if 'a' equals 'b' and 'b' equals 'c'", which is not the case for if(a == b == c) statement.
Hmmm... Didn't you learn something from all the threads you probably have read here during the last two years? How many times have you seen something like this in a beginner's program:
This obviously won't work: Programming languages (maybe except some experimental ones) don't understand everyday's language used in inter-human communication.Code:if (answer == 'y' || 'n')
What you are trying may be seen as a bit less daring since you're "just" trying to make C++ understand some common math jargon idioms. Eventually, however, this won't work either, simply because that's not part of the C++ language. (The language does have these "chained assignments" which look pretty similar but actually are something completely different.)
Chances are that you actually may find a language that understands this, but of course this will not be C++ and so you have to abandon C++ and make a migration. Also, perhaps this language understands your simplified syntax but produces slower code. Are you really willing to save a few keystrokes at the expense of the efficiency of your final program? ;)
Wow, thank you for your condescending replies.
I'm not trying to learn the C++ language, I was simply asking about that specific construct. What shook me is that normally VC compiler gives you a warning if you write something like if(a = 1) but not in this case...
Not mine.
Is a perfectly valid statement. There are many times I do something like:Code:if (a = 1)
Also,Code:if (bQuit = SomeFuncCall())
{
// Do more stuff here
}
is valid as well. As already stated, this will be evaluated left to right, returning "false" in your example.Code:if (a == b == c)
Viggy
I always get surprised how other people are able to find condescension in a trivial reply to a trivial question with an obvious answer. Does it sound condescending to say 'think in French when you write in French'?
If you had a warning thing as a main point, why did you talk about mathematical notation? Don't you find this a bit confusing? ;)Quote:
I'm not trying to learn the C++ language, I was simply asking about that specific construct. What shook me is that normally VC compiler gives you a warning if you write something like if(a = 1) but not in this case...
BTW, it shake me every time how easily you get excited by the things I'd never even notice, or paid really less attention.
You might be right. I think I had a bad day yesterday. It all actually started in another thread when people were treating me like some noob programmer... I guess there are quite a few of them here.
Well, you're definitely not a noob, so why to bother about what others say or think of you. Being positive about yourself is a great thing, almost the same great as being demanding to yourself and forgiving to others. :)
Wow, I didn't know I'd get this kinda encouragement on this site :) It was just too many things going wrong at once... I'm sure you've experienced that... anyway, thanks.