BETWEEN?

Previous topic - Next topic

bigtunacan

Is there an equivalent of a BETWEEN function?  So instead of having to do IF (a>=5 AND a <=10) one can say IF BETWEEN(a,5,10) ?

Obviously pretty easy to roll your own, but it is commonly supported in a lot of langs, so if there is one in GLBasic I would prefer to use it for consistency's sake.

MrTAToad

I've never heard of the command...  And no, there isn't one in GLBasic...

mentalthink

#2
You can make something like this:

FUNCTION BETWEEN: value, range_Beggin, range_End

IF (value>range_Beggin and value<range_End)
             RETURN true
             ELSE
             RETURN false
ENDIF

ENDFUNCTION

I think this have to works, but I´m not tested.



ampos

Quote from: mentalthink on 2011-Oct-22
I think this have to works, but I´m not tested.

Creo que tiene que funcionar, pero yo no estoy comprobado

:D :D :D :D
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Sixth Sense

Quote from: bigtunacan on 2011-Oct-22
Obviously pretty easy to roll your own, but it is commonly supported in a lot of langs

I've been using BASIC for about 30 years now and have never come across this one before, which language did you use it with?
Am I missing the point or is it just as easy to say IF a>4 and a<11 THEN ?
Okay, It's been more than ten years now so I confess............I let the dogs out!

Ruidesco

This is the first time I see a function like that, and I've used quite a few languages. :blink:

matchy

Note that BETWEEN is used an SQL operator for dates also.  :rtfm: I do wonder why it was never a common BASIC word in regardless to readability.


mentalthink

QuoteCreo que tiene que funcionar, pero yo no estoy comprobado

Mira que eres Ca... JAJAJA,  =D =D =D

ya sabés que mi Inglés es de andar por casa... solo unos m2.     :-[ :-[ :-[ :-[

PS: Merci por la corrección...

spacefractal

I have never heard that before in basic lanugage, but its easy to just do a function for that, that does that job. And I guess its not even in C++ too, so better to write that little function instead.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

bigtunacan

I dropped my own function in to do this; I asked because it is fairly common and when possible I'd rather use language built in functions over my own just for consistency.

As to the question of what other languages support it, as Matchy mentioned SQL supports it for one. 
Code (glbasic) Select

SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND value2


Also Ruby
Code (glbasic) Select

4.between?(1,5) // => true
6.between?(1,5) // => false


It shortens the code a bit and makes things more readable; also re-using the same code reduces stupid mistakes.

matchy

Again, in SQL it is an operator not a function and it is catered for time and dates rather than alphanumeric because that is how they can only be conditioned.

Eg. it is illogically to have; if date_stamp > "01/01/2001" and date_stamp < "02/02/2002".


fuzzy70

I have come across that command before in Basic, a very long time ago though on something like AmigaBasic, MacBasic or one of the hundreds of other variants I have used over the years lol.

Quote from: bigtunacan on 2011-Oct-24It shortens the code a bit and makes things more readable; also re-using the same code reduces stupid mistakes.

True, but making a function that simple will not increase code size as to be a problem & also calling it the same name i.e BETWEEN means that the IDE will colour code it as well as show the parameters at the bottom of the screen helping to avoid mistakes.

I tend to build a library of functions that I keep in a directory called Functions funnily enough & just include them when I need them, calling the filename the same as the function name as to help keep things clear & easy to find. Unless the IDE for the language I am using has a code library or code snippets then I use that instead  :)
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)