Kotlin for Loop

Kotlin For Loop – Complete Beginner Guide With Examples
Loops are essential in programming because they allow you to repeat a block of code multiple times without rewriting it. In Kotlin, the for loop is one of the most commonly used looping structures.
If you are learning Kotlin, understanding the for loop will help you work with:
Ranges
Arrays
Lists
Collections
Index-based iteration
In this SEO-optimized, beginner-friendly guide, you’ll learn:
What a for loop is
Basic syntax in Kotlin
Iterating over ranges
Looping through arrays and lists
Using indices
Using step, downTo, and until
Nested for loops
Real-world examples
Common mistakes
Best practices
Let’s get started
What Is a For Loop in Kotlin?
A for loop is used to iterate over a range, array, list, or collection.
It executes a block of code once for each element in the sequence.
Unlike some other languages, Kotlin’s for loop is designed to work naturally with collections and ranges.
Basic Syntax of Kotlin For Loop
Here:
variableholds each valuerangecan be numbers, array, list, etc.
Simple Example – Loop Over Numbers
Output:
Explanation:
1..5creates a range from 1 to 5 (inclusive).
Using until in Kotlin For Loop
The until keyword excludes the last number.
Output:
Difference:
1..5→ includes 51 until 5→ excludes 5
Using step in For Loop
The step keyword controls increment.
Output:
Counting Down with downTo
To loop backward, use downTo.
Output:
You can also combine:
Iterating Over Arrays
Example:
Output:
Iterating Over Lists
This is common in Android development.
Looping with Indices
Sometimes you need index values.
Output:
Using withIndex()
Kotlin provides withIndex() for cleaner code.
This improves readability.
Nested For Loop in Kotlin
You can place one for loop inside another.
Example:
Output:
Nested loops are useful in:
Matrix operations
Pattern printing
Game grids
Real-World Example – Sum of Numbers
Output:
Real-World Example – Finding Even Numbers
Output:
Using break in For Loop
break stops the loop immediately.
Output:
Using continue in For Loop
continue skips current iteration.
Output:
For Loop vs While Loop
| Feature | For Loop | While Loop |
|---|---|---|
| Known range | Best choice | Not ideal |
| Unknown iterations | Not ideal | Best choice |
| Collection iteration | Very easy | Manual |
| Readability | High | Moderate |
Choose wisely based on situation.
Performance Considerations
The for loop in Kotlin:
Is optimized for collections
Avoids index errors
Improves readability
Works well with large datasets
Avoid:
Unnecessary nested loops
Heavy calculations inside loop
Common Beginner Mistakes
Forgetting Range Direction
This won’t work. Use downTo.
Confusing until and ..
Remember:
..includes last valueuntilexcludes it
Index Out of Bounds
Always use .indices or withIndex().
When Should You Use For Loop?
Use for loop when:
Iterating over array or list
Working with fixed range
Processing collection data
Printing patterns
Looping through indexes
Avoid using while loop when range is clearly defined.
Frequently Asked Questions (FAQs)
1. What is a for loop in Kotlin?
A for loop in Kotlin is used to iterate over ranges, arrays, lists, or collections and execute a block of code for each element.
2. What is the difference between .. and until?
The .. operator includes the end value, while until excludes it.
3. How do I loop backward in Kotlin?
Use downTo keyword.
4. Can I use break and continue in for loop?
Yes, both can be used to control loop flow.
5. When should I use for instead of while?
Use for when the number of iterations is known.
Conclusion
The Kotlin for loop is one of the most powerful and frequently used control flow tools.
You learned:
Basic syntax
Ranges and step
downTo and until
Iterating arrays and lists
Nested loops
break and continue
Real-world examples
Mastering the for loop will make your Kotlin programming more efficient and readable.
