Thread: Very elementary C++ question

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    26

    Very elementary C++ question

    Okay, this is a bit basic but I'm having a hard time Googling it so what the hey...

    Does C++ accept logic tests like:

    Code:
    if (a <= x <= y) {}
    Or do I have to keep writing it out as:

    Code:
    if (a <= x && x <= y) {}
    ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes it 'accepts' it (it's valid syntax), but it doesn't do what you want.

    If the latter is what you want, then that's the way to write it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    It's similar to saying:
    Code:
       if (int(a <= b) <= c)
    which is probably never what you intend to say.

    So, yes, you have to separate the expressions with &&.
    Last edited by rudyman; 07-20-2008 at 07:24 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    Oh, okay. So what does the <= operator do in that context?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dondrei View Post
    Oh, okay. So what does the <= operator do in that context?
    Exactly what you would expect from that expression : compares if the result of (a <= b) [which is either 0 or 1] is greater or equal to c.

    The problem is that you didn't want to compare if the boolean value of (a <= b) is greater or equal to c, you wanted to compare if c is greater or equal to b as well as a <= b.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you're doing this sort of thing often, you could make a function out of it.
    Code:
    bool in_range(bool min, bool num, bool max) {
        return min <= num && num <= max;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Dondrei View Post
    Code:
    if (a <= x && x <= y) {}
    I always put parenthesis around my conditions to make is more clear as to what I intended to compare and also so I don't have to keep looking up the operator precedence chart all the time.
    Code:
    if ( (a <= x) && (x <= y) ) {}

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't.
    Code:
    if (a <= x && x <= y) {}
    is perfectly readable as far as I am concerned . . . but if you don't think so, then by all means use parentheses.

    It's a common enough idiom.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by dwks View Post
    I don't.
    Code:
    if (a <= x && x <= y) {}
    is perfectly readable as far as I am concerned . . . but if you don't think so, then by all means use parentheses.

    It's a common enough idiom.
    Yes, that particular example is fine, but it you start getting into more complex expressions and start using different operators, you might run into problems, especially where precedence is concerned. So I choose to always be explicit with parentheses so I don't have to remember when it's safe not to use them...

    I've seen some code that did something like this:
    Code:
    if ( ptr = func() == NULL )
    and from the context it looks like the person who wrote it intended it to mean:
    Code:
    if ( (ptr = func()) == NULL )
    but because of precedence it actually meant:
    Code:
    if ( ptr = (func() == NULL) )

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I know what you mean; I always use parentheses when combining || and &&, for example, even though I know which one comes first in the precedence table . . . .

    That reminds me. Sorry for the change of topic here, but is there anything wrong in using the assignment operator inside if/while statements/loops?
    Code:
    if(c = read())
    while(c = something)
    Without double-parentheses, that is?
    Code:
    if((c = read()))
    while((c = something))
    Turbo C (I know) gives me an error, and GCC a warning (I think), but that's probably just because the compilers thought I meant ==, right?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by dwks View Post
    I know what you mean; I always use parentheses when combining || and &&, for example, even though I know which one comes first in the precedence table . . . .

    That reminds me. Sorry for the change of topic here, but is there anything wrong in using the assignment operator inside if/while statements/loops?
    Code:
    if(c = read())
    while(c = something)
    Without double-parentheses, that is?
    Code:
    if((c = read()))
    while((c = something))
    Turbo C (I know) gives me an error, and GCC a warning (I think), but that's probably just because the compilers thought I meant ==, right?
    I don't think there is anything wrong with it, but I'm pretty sure VC++ gives you a "assignment in conditional" warning because it's so easy to accidentally type = when you really mean ==
    For that I usually get rid of the warning by doing:
    Code:
    if ( (ptr = func()) != NULL )

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yeah, that's what I thought. I usually just put double-parentheses.
    Code:
    if((c = something())
    Of course, I try not to do that very often.

    Anyway, now that we've successfully derailed the thread . . . if you created your own class, I think you could probably overload the <= operator in such a way that "a <= x <= y" works as expected.

    [edit] All I have at the moment is Dinkumware, so this hasn't been tested, but here's sort of how you could do it. I think.
    Code:
    #include <iostream>
    
    class type {
    private:
        int x;
    
    public:
        type(int x) : x(x) {}
        int get() { return x; }
    };
    
    type *operator <= (type *left, type &right) {
        return left->get() <= right.get() ? left : 0;
    }
    
    type *operator <= (type &left, type &right) {
        return left.get() <= right.get() ? &left : 0;
    }
    
    int main() {
        type one(1), two(2), three(3);
    
        if(one <= two <= three) {
            std::cout << "true" << std::endl;
        }
    }
    [/edit]
    Last edited by dwks; 07-24-2008 at 12:06 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    I think I'll just separate with &&s, probably the simplest way.

    Quote Originally Posted by matsp View Post
    Exactly what you would expect from that expression : compares if the result of (a <= b) [which is either 0 or 1] is greater or equal to c.

    The problem is that you didn't want to compare if the boolean value of (a <= b) is greater or equal to c, you wanted to compare if c is greater or equal to b as well as a <= b.

    --
    Mats
    Ah, that makes sense.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by dwks View Post
    Anyway, now that we've successfully derailed the thread . . . if you created your own class, I think you could probably overload the <= operator in such a way that "a <= x <= y" works as expected.
    You can indeed. See Syntactic Aspartame: Recreational Operator Overloading which was published in the Feb 2006 issue of C/C++ User's Journal (the last issue before it's demise).

    --
    Computer Programming: An Introduction for the Scientifically Inclined

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM