View Full Version : syntax- [1 to 100]
Regimos
December 22nd, 2009, 11:52 PM
I've tried a bunch of different methods, but none seem to work. How exactly do you code
if (blah == (1 or 2 or 3 or 4 or 5 or 6 ... 100))
without typing out the whole thing?
Thanks for any help.
Krilnon
December 23rd, 2009, 12:35 AM
You could do something like:
if(blah >= 1 && blah <= 100)
That will work if blah is an int or a uint. It'll also work if it's a Number and you know that you won't have non-integer values like 2.5, or if you don't care if those are included in the range.
Regimos
December 23rd, 2009, 01:10 AM
Ahaha, how did I miss the simplest solution there was? I assumed there was some sort of syntax for it, but this works too. Thanks.
Krilnon
December 23rd, 2009, 12:38 PM
Some other languages have a syntax for ranges, like Ruby, but even in those languages I would probably still use comparison operators instead because it seems like there's a lot more overhead when creating a range and the code clarity difference isn't huge.
>> (1..100).member? 5
=> true
>> (1..100).member? 500
=> false
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.