Introduction
To solve a problem, sometimes, we need to execute the same code multiple times. To achieve this, one way could be to repeat the code simply. Sounds too tiring and time-consuming? Fortunately, there is a better way to do the same. Loops can be used in such a situation.
In this blog, we will discuss for loop, while loop, and do-while loop in Kotlin. We will see where and how each of these loops can be used. We will also explore break and continue expressions and how these expressions can be used with the loops to control the flow of execution of code.
For loop
In Programming, for loop is a conditional iterative statement that is used to check for a particular condition and then executes a block of code continuously as long as those conditions are met. The for loop differs from other looping statements in that it has an explicit loop counter that allows the loop’s body to know the exact sequence of each iteration.
For loop in Kotlin is the same as foreach loop in other languages like C#. It can be used to scan any data structure that provides an iterator.
Syntax of for loop in Kotlin is as follows:
for(element in collection) {
// write the code here
}
We can use a for loop to traverse over a range. For example,
// Traversing over a range using a for-loop
fun main(){
// Iterating from 1 to 5
print("Printing Elements from 1 to 5: ")
for(i in 1..5){
print("$i ")
}
print("\n") // Adding a new line
// Iterating from 1 to 5 backwards
print("Printing Elements from 1 to 5 (top to down): ")
for(i in 5 downTo 1 ){
print("$i ")
}
}
Output:
Printing Elements from 1 to 5: 1 2 3 4 5
Printing Elements from 1 to 5 (top to down): 5 4 3 2 1
In the above example, we have used two for loops. The First one traverses over the range from 1 to 5, and the second one traverses the same range in the opposite direction. It is necessary to use the keyword downTo while traversing a range backward.
We can also traverse an array using a for loop. There are three methods to traverse over an array using a for loop, which are as follows:
- Without using the index of the element
- Using Index of the element
- Using withIndex library function
Let’s look at all these methods in detail with examples.
Without Using Index
In the below example, we traverse over an array without using its index.
// Traversing the elements of an array without using index
fun main(){
// Initialising an array
var arr = arrayOf("a","b","c","d","e","f","g")
for(ch in arr){ // Traversing over the array
println("current character is: $ch")
}
}
Output:
current character is: a
current character is: b
current character is: c
current character is: d
current character is: e
current character is: f
current character is: g
Using Index
We can traverse over an array using the indices of its element. For example,
// Traversing the elements of an array using indices
fun main(){
var cityList = arrayOf("Mumbai","New Delhi","Kolkata", "Gurgaon", "Pune")
for(i in cityList.indices){
println("City: ${cityList[i]}")
}
}
Output:
City: Mumbai
City: New Delhi
City: Kolkata
City: Gurgaon
City: Pune
Using withIndex Library function
In the next example, we traverse over an array using the withIndex library function.
// Traversing the elements of an array using the withIndex Library function
fun main(){
var cityList = arrayOf("Mumbai","New Delhi","Kolkata", "Gurgaon", "Pune")
for((index,value) in cityList.withIndex()){
println("The city at $index th index is: $value")
}
}
Output:
The city at 0 th index is: Mumbai
The city at 1 th index is: New Delhi
The city at 2 th index is: Kolkata
The city at 3 th index is: Gurgaon
The city at 4 th index is: Pune




