CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: if(a == b == c)

  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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++;
    	}

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    [...] 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.

  3. #3
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    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
    Code:
    true == c
    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.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    And no, your assumption is not correct. The first if statement will result in a false.
    Quote Originally Posted by Paul McKenzie View Post
    a == b is true. Then
    Code:
    true == c
    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.

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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.

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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.

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    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

  9. #9
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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...

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: if(a == b == c)

    Not mine.
    Code:
    if (a = 1)
    Is a perfectly valid statement. There are many times I do something like:
    Code:
    if (bQuit = SomeFuncCall())
    {
       // Do more stuff here
    }
    Also,
    Code:
    if (a == b == c)
    is valid as well. As already stated, this will be evaluated left to right, returning "false" in your example.

    Viggy

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    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

  12. #12
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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.

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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

  14. #14
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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.

  15. #15
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: if(a == b == c)

    Quote Originally Posted by ahmd View Post
    If it's a wrong section, [...]
    [ Moved thread ]
    Last edited by ovidiucucu; June 12th, 2011 at 11:11 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured