Kotlin for Loop

Kotlin Tutorial

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:

  • variable holds each value

  • range can be numbers, array, list, etc.


Simple Example – Loop Over Numbers

Output:

1
2
3
4
5

Explanation:

  • 1..5 creates a range from 1 to 5 (inclusive).


Using until in Kotlin For Loop

The until keyword excludes the last number.

Output:

1
2
3
4

Difference:

  • 1..5 → includes 5

  • 1 until 5 → excludes 5


Using step in For Loop

The step keyword controls increment.

Output:

1
3
5
7
9

Counting Down with downTo

To loop backward, use downTo.

Output:

5
4
3
2
1

You can also combine:


Iterating Over Arrays

Example:


 

Output:

10
20
30

Iterating Over Lists


 

This is common in Android development.


Looping with Indices

Sometimes you need index values.


 

Output:

Index 0 = Red
Index 1 = Green
Index 2 = Blue

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:

i=1, j=1
i=1, j=2
i=2, j=1
i=2, j=2
i=3, j=1
i=3, j=2

Nested loops are useful in:

  • Matrix operations

  • Pattern printing

  • Game grids


Real-World Example – Sum of Numbers


 

Output:

Sum: 15

Real-World Example – Finding Even Numbers

Output:

2
4
6
8
10

Using break in For Loop

break stops the loop immediately.

Output:

1
2
3
4

Using continue in For Loop

continue skips current iteration.

Output:

1
2
4
5

For Loop vs While Loop

FeatureFor LoopWhile Loop
Known rangeBest choiceNot ideal
Unknown iterationsNot idealBest choice
Collection iterationVery easyManual
ReadabilityHighModerate

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

for (i in 5..1)

This won’t work. Use downTo.


 Confusing until and ..

Remember:

  • .. includes last value

  • until excludes 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.

You may also like...