Kotlin for Loop

Before we jump on see what Kotlin has to offer in terms of for loop, let’s first recap the one we have used in Java.

A typical Java for loop would like below:

for (int i = 0;i<10;i++){
	System.out.println("Number is " + i);
}

Kotlin for Loop

The above code can be divided into four parts:

  • Declaration Of Variable: The count variable which you want to use in the conditional loop. Here in the example we have used it as i and is of the type “Integer”.
  • Condition Of Loop: The exit criteria or the condition it can be called or referred in simple terms. Here we have simply classified that as i<10.
  • Increment / Decrement of Count: What is factor by which you would like to revive your loop? To be precise condition to update the count. In the example demonstrated above we can refer i++.
  • Body of the Loop: The lines of code you would like to use or execute in continuation. We generally separate it by braces {} and if the loop is having a single line of code we simply mention it.

Kotlin for Loop

Kotlin for loop does exactly the same for us. It provides you the functionality to rerun the same lines of code again and again but has certain advantages which reduce the code making it easier for the developer and hence improves efficiency.

Before we demo the Kotlin’s for, let’s have a look at some unique features offered by the Kotlin language:

Kotlin Range

When defining the limit or the exit criteria in case of Kotlin, we can use a simple operator which defines from condition and the exit condition.

So .. is a range in simple terms it point to both the number being inclusive.

So 1..10 would mean number from 1 till 10.

To check if a number exists in the range or not , Kotlin has given us another keyword which will help us identify if the number falls in the mentioned range or not and that keyword, we call it as in.

To check if a number x say belongs to range we can simply use it as:

x in 1..10 (this would return if the variable x has value in between the range)

In certain cases if you wish to use check if the number doesnot exist in the range a simple not (!) operator would be helping us.

To check if the x doesnot belongs to range 1 to 10, we will write as:

x !in 1..10 (As explained, this code would check x’s value against the number and return true if it doesnot exists).

So from the above evidence it would be clear how simple it would be to write a simple for loop to print say first 10 natural numbers. So, this is how short this code would be as:

for (x in 0..10){
	print(x)
}

The above code would be looking pretty simple and that it actually is.

But if you remember a basic program lets say print first ten even number in that case we alter the for loop by incrementing more than one (i.e. i=i+2). Now how this case would be catered.

In case if you wish to change or provide an increment other than one, you can do it by using step as below:

for (x in 1..20 step 2){
	print(x)
}

The above code will print first 10 odd numbers.

But, what if in certain case or if I want to use this loop in the reverse chronological order what would be our approach let have a look at that.

Yes, you have thought it right range doesn’t supports reverse ranges if you wish to use them in your code. So now to solve this problem, kotlin came up with the concept of step keyword.

In such a case Kotlin has provided its functionality in which we can actually use the down word to decrement the value of count variable but mind you, range would still be same. Let’s have a look at a for loop with a reverse range.

For this, Kotlin uses the downTo keyword. As it can be very well understood the down keyword will be helpful to reduce the count value when moving in the iteration factor.

for (i in 10 downTo 1){
	print(i)
}

Above code will print number in reverse order.

So, that’s the benefit of using Kotlin’s for loop which offers ease and reduces the effective lines of code saving developer’s time and effort.

But if you have a complex calculation coming your way, then it’s advisable to use the Kotlin’s while loop.

In the next tutorial, we would see how can we use the while loop in Kotlin and what are the perks it has to offer.

Leave a Comment

Your email address will not be published. Required fields are marked *