is there short hand when evaulating if a value is between/within two values?
ie, instead of
if ((value1 > value 2) && (value1 < value3)) {}
is there short hand when evaulating if a value is between/within two values?
ie, instead of
if ((value1 > value 2) && (value1 < value3)) {}
not that I can think of, except you shouldn't need so many brackets:
Code:if (value1 > value2 && value1 < value3) {}
if i always wondered if there was a shorthand for evaluating an integer against being between two other integers resulting in a boolean perhaps,
ie, if (value1[value2,value3]) {}
On paper I would probably write
I don't know of any programming language that lets you write the statement more efficiently though, however there maybe one that provides a function like:Code:(value2<value1<value3)
(and if there isn't you could write your own)Code:is_between(value1, value2, value3)
I don't think you would gain anything with it though. At the end of the day to work out whether one number is between two others a computer has to do two separate comparisons, and I expect the most efficient and quick way of writing that is still:
And as a human being trying to read the code, it also seems better having the comparison written clearly and obviously.Code:(value1>value2 && value1<value3)
Last edited by nick; 5th April 2007 at 01:14 PM. Reason: couldn't spell
guess that's a no then.
like you say, if it's easy to read, it's probably the only option!
that's the code that had me asking questions sol!!
taping away using between on an sql query, i wondered... (waving retro dream like... waves)...
'do other programming languages use this short hand?'
the answer so far is no (for the ones i use, anyhow)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks