Welcome to The Coding College, your ultimate resource for coding and programming knowledge. In this guide, we’ll explore the Kotlin For Loop, an essential control structure for iterating over collections and ranges.
What is a For Loop in Kotlin?
A for loop in Kotlin is used to execute a block of code repeatedly for a specified number of times. It is often used to iterate through collections like arrays, lists, or ranges.
Syntax of Kotlin For Loop
for (item in collection) {
// Code to execute for each item
}
Iterating Over a Range
The range keyword helps define a sequence of numbers.
Example:
fun main() {
for (i in 1..5) {
println(i)
}
}
// Output: 1 2 3 4 5
Explanation:
1..5
creates a range from 1 to 5.i
takes each value in the range.
Using the step
Keyword
You can control the increment of the loop using the step
keyword.
Example:
fun main() {
for (i in 1..10 step 2) {
println(i)
}
}
// Output: 1 3 5 7 9
Iterating in Reverse
The downTo
keyword allows iterating in reverse.
Example:
fun main() {
for (i in 5 downTo 1) {
println(i)
}
}
// Output: 5 4 3 2 1
You can combine downTo
with step
to customize the decrement.
Example:
fun main() {
for (i in 10 downTo 1 step 2) {
println(i)
}
}
// Output: 10 8 6 4 2
Iterating Over Arrays
You can iterate through elements of an array using the for loop.
Example:
fun main() {
val fruits = arrayOf("Apple", "Banana", "Cherry")
for (fruit in fruits) {
println(fruit)
}
}
// Output: Apple Banana Cherry
Iterating with Indices
To access the index of each element, use the indices
property or withIndex()
function.
Using indices
:
fun main() {
val fruits = arrayOf("Apple", "Banana", "Cherry")
for (index in fruits.indices) {
println("Index $index: ${fruits[index]}")
}
}
// Output:
// Index 0: Apple
// Index 1: Banana
// Index 2: Cherry
Using withIndex()
:
fun main() {
val fruits = arrayOf("Apple", "Banana", "Cherry")
for ((index, fruit) in fruits.withIndex()) {
println("Index $index: $fruit")
}
}
// Output:
// Index 0: Apple
// Index 1: Banana
// Index 2: Cherry
Practical Examples
Example 1: Summing Numbers in a Range
fun main() {
var sum = 0
for (i in 1..10) {
sum += i
}
println("Sum: $sum")
}
// Output: Sum: 55
Example 2: Filtering Even Numbers
fun main() {
for (i in 1..10) {
if (i % 2 == 0) {
println(i)
}
}
}
// Output: 2 4 6 8 10
Example 3: Nested Loops
fun main() {
for (i in 1..3) {
for (j in 1..3) {
println("i=$i, j=$j")
}
}
}
// Output:
// i=1, j=1
// i=1, j=2
// i=1, j=3
// i=2, j=1
// ...
Best Practices for Using For Loops
- Use the Right Loop: If you don’t need an index, use
for
directly with collections. - Optimize Range Usage: Use
step
anddownTo
for precise control. - Avoid Nested Loops: If possible, avoid too many nested loops for better readability and performance.
Learn More on The Coding College
Mastering loops is essential for every Kotlin programmer. Whether you’re iterating over ranges or processing arrays, the for loop in Kotlin simplifies repetitive tasks.
Check out more tutorials on The Coding College for a deeper dive into Kotlin and other programming concepts.
Conclusion
The for loop in Kotlin is a versatile and powerful tool for iterating over ranges, arrays, and collections. By mastering its usage, you can write cleaner, more efficient code.