More than and less than

This is a discussion on More than and less than within the C++ Programming forums, part of the General Programming Boards category; I have to use more than and less than together very often: Code: if(myint>=10 && myint<=20){ //do something } Is ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    More than and less than

    I have to use more than and less than together very often:
    Code:
    if(myint>=10 && myint<=20){
    //do something
    }
    Is there a special operator for this?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Just make a bool function (or an int ... -1, 0, 1 with -1 for them being equal or something), which tests for what you're looking for.

    Code:
    #include <iostream>
    
    bool thing( int var, int lower, int upper ) 
    {
    	return ( (var>=lower) && (var<=upper) );
    }
    
    int main( void )
    {
    	for ( int i=0; i<10; i++ )
    	{
    		std::cout<< thing( i, 4, 7 ) << "\n";
    	}
    
    	return 0; 
    }
    Last edited by twomers; 10-07-2006 at 02:53 PM.

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Did you mean like one symbol, say @ or ~ representing >= or <=? If that is what you meant than no, I don't think that there is one character for that. Also, the gain would only be the time it takes to type one character and your code would be less readable to those that didn't know the method. My recommendation is to stick to the standard.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Compilers offer extensions. Some related to operators. You may want to check yours. Otherwise, echo twomers... with maybe a template wrapped around it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If it's not in the standard, then I don't want to use it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    in my opinion tests like this tend to be more readable if you don't change the direction of the comparisons and write "myint is between 10 and 20" as "10 <= myint && myint <= 20". It looks more like the popular mathematical notation.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This has no point. Your comparision "if under 11 AND under 21" which are all numbers less than 21.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by maxorator
    This has no point. Your comparision "if under 11 AND under 21" which are all numbers less than 21.
    No, it ain't. If you find that hard to read, just use whatever you are used to.


    Either just use the compound evaluation, or make a function like isInRange like already suggested.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Oops, I didn't notice they switched sides, sorry.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    [THIS IS ME BEING STUPID]
    Actually on Dev-C++ (Which uses MinGW), this works for me, which I find spectacular and never used before until I tested it. I don't know if this is standard C++, but it's just like a mathematical expression.
    Code:
    if (10 <= myInt <= 20)
    Would anyone mind running this one by the standard? I didn't know those comparison operators could be stringed.
    [/THIS IS ME BEING STUPID]

    Edit: Ah... what the hell am I thinking. I probably should have immediately realized that the second operator was comparing a Boolean 1 to be <= 20. I probably should have done a check where the first expression returned false. I need my stupid pills... Anyway, it'd be super cool if somehow the implementation would allow that expression. Perhaps some sort of dereferencing of the Boolean result that would yield the actual "greater than" value.
    Last edited by SlyMaelstrom; 10-08-2006 at 05:55 AM.
    Sent from my iPad®

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    Actually on Dev-C++ (Which uses MinGW), this works for me
    It "works" as in it compiles, or "works" as in it is the equivalent of this?
    Code:
    if (10 <= myInt && myInt <= 20)
    As far as I can tell, it should always evaluate to true.

    Would anyone mind running this one by the standard? I didn't know those comparison operators could be stringed.
    Consider section 4.5 of the C++ Standard:
    "An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one."

    Now, (10 <= myInt) evaluates to either false or true. This then can be converted to 0 or 1. Since (0 <= 20) is true and (1 <= 20) is true, the expression (10 <= myInt <= 20) is always true.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Hah... I noticed before you could type out your whole post! That either means that I'm not as dumb as I really am or you just type slow... or both... or I forget... my head hurts. In any event, stupid is as stupid does.

    Code:
    myBrain <= LaserlightsWPM <= timeINoticed;
    Last edited by SlyMaelstrom; 10-08-2006 at 05:57 AM.
    Sent from my iPad®

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > if (10 <= myInt <= 20)
    Which becomes
    if ( (10 <= myInt) <= 20)
    The red sub-expression is evaluated, and the boolean result (0 or 1) is then compared with 20 (always going to be true).

    Valid - yes.
    What you wanted - probably not.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    I said I noticed. What more do you want from me.

    Something tells me I'm going to be made aware of this mistake several more times.
    Sent from my iPad®

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    sorry - missed your edit
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21