|
-
June 9th, 2011, 06:12 PM
#1
if(a == b == c)
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++;
}
-
June 9th, 2011, 06:47 PM
#2
Re: if(a == b == c)
 Originally Posted by ahmd
[...] is comparison 1 evaluating to comparison 2 or 3?
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?
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
June 9th, 2011, 07:32 PM
#3
Re: if(a == b == c)
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.
-
June 9th, 2011, 08:32 PM
#4
Re: if(a == b == c)
 Originally Posted by ahmd
It said, "C++ and WinAPI", so I'm taking it as C++ (since there's no C section here).
C++ precedence rules aren't Windows API issues.
http://www.cppreference.com/wiki/lan...tor_precedence
The first if statement will result in a false.
a == b is true. Then
is false, since c is 0, so the entire expression is false.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 10th, 2011 at 02:01 AM.
-
June 9th, 2011, 08:42 PM
#5
Re: if(a == b == c)
 Originally Posted by ahmd
And no, your assumption is not correct. The first if statement will result in a false.
 Originally Posted by Paul McKenzie
a == b is true. Then
is fakse, since c is 0, so the entire expression is false.
D'oh! That's what happens when I make conclusions too quickly... 
But, ahmd, if you already knew that, why then did you ask? Did you intend to test us?
Last edited by Eri523; June 10th, 2011 at 04:43 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
June 9th, 2011, 09:13 PM
#6
Re: if(a == b == c)
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.
-
June 9th, 2011, 10:32 PM
#7
Re: if(a == b == c)
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:
Code:
if (answer == 'y' || 'n')
This obviously won't work: Programming languages (maybe except some experimental ones) don't understand everyday's language used in inter-human communication.
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?
Last edited by Eri523; June 10th, 2011 at 02:33 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
June 10th, 2011, 02:01 PM
#8
Re: if(a == b == c)
 Originally Posted by ahmd
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.
C is not a mathematician's language, that's what you should keep in mind at coding time. This habit will save your debugging time which cost is much bigger than coding time.
Best regards,
Igor
-
June 10th, 2011, 02:55 PM
#9
Re: if(a == b == c)
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...
-
June 10th, 2011, 02:57 PM
#10
Re: if(a == b == c)
Not mine.
Is a perfectly valid statement. There are many times I do something like:
Code:
if (bQuit = SomeFuncCall())
{
// Do more stuff here
}
Also,
is valid as well. As already stated, this will be evaluated left to right, returning "false" in your example.
Viggy
-
June 11th, 2011, 03:06 PM
#11
Re: if(a == b == c)
 Originally Posted by ahmd
Wow, thank you for your condescending replies.
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'?
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...
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? 
BTW, it shake me every time how easily you get excited by the things I'd never even notice, or paid really less attention.
Best regards,
Igor
-
June 11th, 2011, 03:26 PM
#12
Re: if(a == b == c)
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.
-
June 11th, 2011, 04:30 PM
#13
Re: if(a == b == c)
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.
Best regards,
Igor
-
June 11th, 2011, 06:10 PM
#14
Re: if(a == b == c)
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.
-
June 12th, 2011, 10:40 AM
#15
Re: if(a == b == c)
 Originally Posted by ahmd
If it's a wrong section, [...]
[ Moved thread ]
Last edited by ovidiucucu; June 12th, 2011 at 11:11 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|