Ranges in Scala can be declared via using the to
and until
methods. The to
method is inclusive of the upper bound whereas the until
method is exclusive. It is therefore recommended that you use the until
method when counting upto a length
-like property. By doing so, you don't have to worry about handling the off-by-one errors yourself in most cases.
for (i <- 0 to container.length) {
// ...
//
// The following check becomes redundant when `until` is used.
if (i == container.length) {
// Break?
}
}
if (i <- 0 until container.length) {
// ...
}