Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if a variable is within range?

I need to write a loop that does something like:

if i (1..10)   do thing 1 elsif i (11..20)   do thing 2 elsif i (21..30)   do thing 3 etc... 

But so far have gone down the wrong paths in terms of syntax.

like image 490
btw Avatar asked May 15 '09 19:05

btw


People also ask

How do you know if a number is within a range?

The idea is to multiply (x-low) and (x-high). If x is in range, then it must be greater than or equal to low, i.e., (x-low) >= 0.

How do you see if a number is within a range Java?

ValueRange. of(minValue, maxValue); range. isValidIntValue(x); it returns true if minValue <= x <= MaxValue - i.e. within the range.


2 Answers

 if i.between?(1, 10)   do thing 1  elsif i.between?(11,20)   do thing 2  ... 
like image 105
rogerdpack Avatar answered Nov 10 '22 06:11

rogerdpack


Use the === operator (or its synonym include?)

if (1..10) === i 
like image 22
Baldu Avatar answered Nov 10 '22 05:11

Baldu