Range and Iteration

Reading Time: 3 minutes

In this blog, we are going to learn about ranges and iteration in kotlin. Imagine telling someone to count from one to five by uttering “set i equal to 1 but while keeping i less than 6, increment i and report the value.” If we had to communicate with a fellow human that way, it would have ended civilization a long time ago. Yet that’s how programmers have been writing code in many C-like languages. But we don’t have to, at least not in Kotlin.

Range Classes

Kotlin raises the level of abstraction to iterate over a range of values with specialized classes. For instance, here’s a way to create a range of numbers from 1 to 5.

val oneToFive: IntRange = 1..5

The type IntRange, which is part of the kotlin.ranges package, is provided for clarity, but you may leave it out and let type inference figure out the variable’s type.

If you want a range of letters in the English alphabet, the process is the same:

val aToE: CharRange = 'a'..'e'

You’re not limited to primitives like int , long , and char . Here’s a range of strings:

val seekHelp: ClosedRange<String> = "hell".."help"

Take the initial value, place the .. operator, followed by the last value in the range. The range includes both the values before and after the .. operator. Let’s quickly check if a couple of values exist in that range.

println(seekHelp.contains("helm")) //true
println(seekHelp.contains("helq")) //false

The first call verifies that the range includes the value “helm” , which is in the lexical order of words between the values “hell” and “help” . The second call confirms that the value “helq” is not in the range.

Forward Iteration

Once we create a range, we can iterate over it using the for (x in ..) syntax. Let’s iterate over a range of values 1 to 5 .

for (i in 1..5) { print("$i, ") } //1, 2, 3, 4, 5

Likewise we can iterate over the range of characters:

for (ch in 'a'..'e') { print(ch) } //abcde

All that went well, but the iteration from “hell” to “help” , like the following, will run into issues:

for (word in "hell".."help") { print("$word, ") } //ERROR

//for-loop range must have an ‘iterator()’ method

The reason for the failure is whereas classes like IntRange and CharRange have an iterator() function/operator, their base class ClosedRange<T> doesn’t.

Reverse Iteration

The previous examples showed iterating forward, but we should be able to iterate over the range in reverse just as easily. Creating a range of values 5..1 won’t cut it, though. This is where downTo comes in.

for (i in 5.downTo(1)) { print("$i, ") } //5, 4, 3, 2, 1,

Both .. and downTo() produced a range of every single value from the start to the end value. It’s not uncommon to skip some values in the range, and there are other methods to achieve that.

Skipping Values in Range

When iterating over a range of numbers, you can skip the ending value in the range by using until() .

for (i in 1 until 5) { print("$i, ") } //1, 2, 3, 4,

This iteration created using until() didn’t include the ending value 5 , whereas the range we created previously using .. did.

To skip some values during iteration, Kotlin provides a step() method. Let’s use the step() method.

for (i in 1 until 10 step 3) { print("$i, ") } //1, 4, 7,

The step() method transforms an IntRange or IntProgression created using .. , until , downTo , etc. into an IntProgression that skips some values. Let’s use step() to iterate in reverse order while skipping some values:

for (i in 10 downTo 0 step 3) { print("$i, ") } //10, 7, 4, 1,

That was an easy way to methodically skip some values, but there are other methods to skip values that don’t fall into a rhythm. For example, if you want to iterate over all values divisible by 3 and 5 , use the filter() method:

for (i in (1..9).filter { it % 3 == 0 || it % 5 == 0 }) {
print("$i, ") //3, 5, 6, 9,
}

The filter() method takes a predicate—a lambda expression—as an argument.

For more information visit:https://kotlinlang.org/docs/ranges.html

Checkout more Kotlin Blogs here.

Scala Future

Written by 

Mohd Uzair is a Software intern at Knoldus. He is passionate about java programming. He is recognized as a good team player, a dedicated and responsible professional, and a technology enthusiast. He is a quick learner & curious to learn new technologies. His hobbies include watching movies, surfing youtube, playing video games.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading