Kotlin for Loop
Kotlin for Loop
In Kotlin, the for loop is used to iterate over ranges, arrays, lists, strings, and other collections. It is simple, powerful, and more flexible than traditional loops.
1. Basic for Loop (Range)
Output:
2. for Loop with until
Output:
(5 is excluded)
3. for Loop with step
Output:
4. Reverse Loop (downTo)
5. Loop Through an Array
6. Loop with Index
7. Using withIndex()
8. Loop Through a String
9. break and continue in for Loop
10. Nested for Loop
Summary
-
forloop iterates over ranges and collections -
No traditional
for(init; condition; update) -
Supports
step,downTo,until -
Clean and readable syntax
