Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Ninjas!! You must have heard about the very popular language used for Android Development ; Kotlin. It is an open-source language developed by JetBrains. With the help of Kotlin, there has been a boost in developing android applications.
Today we will look into the details of Kotlin for loop. Let's get into the basics of Kotlin for loop.
Kotlin for loop
The Kotlin for loop is used to iterate over a collection of items. The in operator is used with the for loop to iterate over each item present in the collection, arrays, etc. Let's look at the syntax of Kotlin for loop.
Syntax:
for (elements in collection) {
// body of loop
}
Iterate through a Range
Let's look at various examples to understand iterating through a range of numbers using Kotlin for loop.
Code:
fun main() {
println("Iterating through a range of 40 to 45")
for (items in 40..45)
println(items)
}
Output:
Iterating through a range of 40 to 45
40
41
42
43
44
45
Explanation:
The loop iterates through a range of 40 to 45 and prints every number.
You can also iterate through the range by skipping the numbers. This can be done using the step keyword used within the for() loop.
Code:
fun main() {
println("Iterating through a range of 40 to 45 with 2 steps")
for (items in 40..45 step 2)
println(items)
}
Output:
Iterating through a range of 40 to 45 with 2 steps
40
42
44
Explanation:
The loop iterates through a range of 40 to 45 and prints the number after 2 steps.
You can also backward iterate through the range of numbers. This is done using the downTo keyword.
Code:
fun main() {
println("Backward Iterating through a range of 45 to 40")
for (i in 45 downTo 40)
println(i)
}
Output:
Backward Iterating through a range of 45 to 40
45
44
43
42
41
40
Explanation:
The loop iterates backward through a range of 45 to 40. The downTo keyword is used for backward iterating through a range.
You can also backward iterate through the range by skipping the numbers. This can be done using the step keyword used within the for() loop.
Code:
fun main() {
println("Backward Iterating through a range of 45 to 40 with 3 steps")
for (i in 45 downTo 40 step 3)
println(i)
}
Output:
Backward Iterating through a range of 45 to 40 with 3 steps
45
42
Explanation:
The loop iterates backward through a range of 45 to 40 and prints every 3rd number.
Iterate through an Array
An array is a collection of similar data-type items stored at contiguous memory locations.
Let's look at various examples to understand iterating through an array using Kotlin for loop.
Code:
fun main() {
var array = arrayOf("Hello!!", "Ninjas", "Happy", "Coding")
println("The items in the array are:")
for (items in array)
println(items)
}
Output:
The items in the array are:
Hello!!
Ninjas
Happy
Coding
Explanation:
The arrayOf() method is used to convert a list of items to an array. The loop iterates through each element present in the array and prints them.
You can also iterate over the array using the index value of the elements in the array.
Code:
fun main() {
var array = arrayOf("Hello!!", "Ninjas", "Happy", "Coding")
println("The items in the array are:")
for (items in array.indices)
println(array[items])
}
Output:
The items in the array are:
Hello!!
Ninjas
Happy
Coding
Explanation:
The program iterates using the indices of elements of the array. The array.indices give the index value rather than the element value.
Iterate through a String
String is defined as a collection of characters that are enclosed within quotes.
Let's look at various examples to understand iterating through a string using Kotlin for loop.
Code:
fun main() {
var string = "Ninjas"
println("The letters in the string are:")
for (letter in string)
println(letter)
}
Output:
The letters in the string are:
N
i
n
j
a
s
Explanation:
The program iterates through every letter of the string and prints them.
You can also iterate over the array using the index value of the letters in the string.
Code:
fun main() {
var string = "Ninjas"
println("The letters in the string are:")
for (letter in string.indices)
println(string[letter])
}
Output:
The letters in the string are:
N
i
n
j
a
s
Explanation:
The program iterates using the indices of letters of the string. The string.indices gives the index value rather than element value.