Kotlin For Loops & Ranges: Iterate Like a Pro in 2025!

For loops are your go-to for sweeping through arrays and ranges effortlessly.

They make repetitive tasks a breeze—perfect for arrays and more! 🌟

Loop through array elements with a for loop and the in operator:



fun main() {
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
for (x in cars) {
println(x)
}
} // Outputs: Volvo, BMW, Ford, Mazda 🚗

For Loop Basics:

  • 🔄 Iterates over arrays or ranges smoothly.
  • 🔍 Uses in to step through each item.
  • ⚡ No need for manual counters—Kotlin does it for you!
  • ✅ Perfect for simple, clean loops.

Traditional For Loop? Not Here! 🛑

Unlike Java, Kotlin skips the classic for (int i = 0; i < n; i++) syntax.

Instead, it uses for with in for arrays, ranges, and anything countable—sleeker and smarter!

Why Kotlin’s Way Wins:

  • 📏 Focuses on what you loop, not how.
  • ⚡ Reduces boilerplate code.
  • 🔄 Adapts to modern iterable needs.

Kotlin Ranges 📏

Use the .. operator with a for loop to create and loop through ranges of values:

fun main() {
for (nums in 5..15) {
println(nums)
}
} // Outputs: 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 📈

Range Power:

  • 📏 start..end includes both ends (inclusive).
  • 🔄 Loops over numbers or even chars (e.g., 'a'..'z').
  • ⚡ Quick way to generate sequences.
  • ✅ Ties in perfectly with for.

Check if a Value Exists 🔎

The in operator also checks if a value is in a range or array:

fun main() {
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
if ("Volvo" in cars) {
println("It exists!") // This runs! ✅
} else {
println("It does not exist.")
}
}

Checking Smarts:

  • 🔎 in works for both ranges and arrays.
  • ✅ Returns true/false—great for conditions.
  • ⚡ No manual search needed.

Break or Continue a Range ⏭️🛑

Add break or continue to control range loops:

This skips 10 and continues:

fun main() {
for (nums in 5..15) {
if (nums == 10) {
continue
}
println(nums)
}
} // Outputs: 5, 6, 7, 8, 9, 11, 12, 13, 14, 15 📈

Control Flow:

  • ⏭️ continue skips an iteration.
  • 🛑 break exits the loop early.
  • ⚡ Fine-tunes what gets processed.

Looping with Indices 🔢

Need the index? Use .indices or withIndex() with your for loop:

This prints cars with their positions:

fun main() {
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
for ((index, value) in cars.withIndex()) {
println("Car #$index: $value")
}
} // Outputs: Car #0: Volvo, Car #1: BMW, Car #2: Ford, Car #3: Mazda 🚗

Index Magic:

  • 🔢 .indices gives 0..size-1.
  • 📏 withIndex() pairs index and value.
  • ⚡ Tracks position without extra variables.

Step and DownTo Ranges ⬇️⬆️

Customize ranges with step or downTo for more control:

This counts down with a step of 2:

fun main() {
for ( nums in 10 downTo 1 step 2) {
println(nums)
}
} // Outputs: 10, 8, 6, 4, 2 ⬇️

Range Tweaks:

  • ⬇️ downTo counts backwards.
  • ⬆️ step skips values (e.g., every 2).
  • ⚡ Combines for custom sequences.

..

Comments

Popular posts from this blog

Creating Beautiful Card UI in Flutter

Master Web Development with Web School Offline

Jetpack Compose - Card View