I have to use more than and less than together very often:
Is there a special operator for this?Code:if(myint>=10 && myint<=20){ //do something }
I have to use more than and less than together very often:
Is there a special operator for this?Code:if(myint>=10 && myint<=20){ //do something }
"The Internet treats censorship as damage and routes around it." - John Gilmore
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.
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.
Compilers offer extensions. Some related to operators. You may want to check yours. Otherwise, echo twomers... with maybe a template wrapped around it.
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.
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
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.
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
No, it ain't. If you find that hard to read, just use whatever you are used to.Originally Posted by maxorator
Either just use the compound evaluation, or make a function like isInRange like already suggested.
Oops, I didn't notice they switched sides, sorry.
"The Internet treats censorship as damage and routes around it." - John Gilmore
[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.Would anyone mind running this one by the standard? I didn't know those comparison operators could be stringed.Code:if (10 <= myInt <= 20)
[/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Ž
It "works" as in it compiles, or "works" as in it is the equivalent of this?Actually on Dev-C++ (Which uses MinGW), this works for me
As far as I can tell, it should always evaluate to true.Code:if (10 <= myInt && myInt <= 20)
Consider section 4.5 of the C++ Standard:Would anyone mind running this one by the standard? I didn't know those comparison operators could be stringed.
"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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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Ž
> 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 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Ž
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.