Given is an integer with the value 5. Given is a range with start and end values of 2 and 5. So is the integer 5 now between the range? What should a 'InBetween()' method return and which one is the correct representation of 'between':
int val = 5
if(val >= 2 && val <= 5) // would return true
if(val > 2 && val < 5) // would return false
Best Answer
There is no clear answer to this as "between" can be interpreted in lots of way. I'd argue that
inBetween(1, 4)
should check 2, 3 but that's just me.There are other examples like,
Random.nextInt(1,4)
- does it return values 1,2,3,4; 2,3 or 2,3,4?So most functions tend to clarify that in the documentation, for instance:
public String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. Examples:
Parameters:
If you want to be precise, then use the common mathematical notation as described here:
So
[1, 5)
means the interval 1,2,3,4 (integers) or 1 to 4.999... if we're using floats.