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
  • 4
    As an aside, it is common to write the above statements in the form (2 <= val && val <= 5) to make it easier to read. – Scroog1 Aug 7 '13 at 8:14

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:

 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"


  beginIndex - the beginning index, inclusive.
  endIndex - the ending index, exclusive.

If you want to be precise, then use the common mathematical notation as described here:

enter image description here

So [1, 5) means the interval 1,2,3,4 (integers) or 1 to 4.999... if we're using floats.

| improve this answer | |
  • This is a great explaination and I'll stick with the 'clarify in documentation' part, altough I guess I'd never write something like [1,5) because it looks more like a parentheses typo which is awfully confusing. – Atrotygma Aug 7 '13 at 8:57
  • In the case of substring (or integer intervals in general), one could disambiguate the upper bound by replacing the parameter that specifies the upper bound by one that specifies the interval's length instead, e.g. a parameter called length or count. – stakx Aug 7 '13 at 10:23

For integer types, it's normal practice to include the specified values within the range. You would say that the range of a single standard die is between 1 and 6, not 0 and 7.

For floating point types, it varies depending on the specification of the particular project. When defining adjacent bands, it's common to include one value, but to exclude the other.


double value
if (value >= 0.0 && value < 2.0)
else if (value >= 2.0 && value < 5.0)
| improve this answer | |

In math, there are four interval (range) specification forms, shown here with corresponding C expressions:

(a,b) ((a < x) && (x < b))
[a,b) ((a <= x) && (x < b))
(a,b] ((a < x) && (x <= b))
[a,b] ((a <= x) && (x <= b))

The idea is that ( and ) denote open ends, while [ and ] denote closed ends.

Added, based on the comment from "stakx": An open end of an interval is one that does not include the actual endpoint. A closed end is one that does include the endpoint. For example, (3,5) is an open interval, 3 < x < 5. Neither 3 nor 5 are included. [4,6) is a half-open interval: 4 is included, 6 is not. [5,7] is a closed interval: both 5 and 7 are included.

The different forms are not that interesting for integers, as any open or half-open integer interval can be expressed as the equivalent closed interval (and vice versa, of course). They become useful for rationals and reals.

Saying "in between" is ambiguous. It doesn't say whether the endpoints are included (i.e., is the range open or closed on either or both ends). You need to specify it more clearly, so you can say which of the four forms is intended as the specification, given the endpoints.

| improve this answer | |
  • I'm not certain whether your use of the terms "open" and "closed" is well understood by maths laypersons. Referring to the same ideas as "exclusive" and "inclusive" might be a little easier to understand. – stakx Aug 7 '13 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.