100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Loops in Kotlin (for, while, do-while)

Understand Kotlin's for, while, and do-while loops, along with break, continue, and labeled loops.

Control FlowBeginner8 min readJul 8, 2026
Analogies

Introduction

Kotlin provides three loop constructs: for, while, and do-while. The for loop is designed to iterate over anything that provides an iterator - collections, arrays, or ranges - rather than the traditional C-style for (int i=0; i<n; i++) found in Java. Instead, Kotlin uses ranges like 1..5 to express counted iteration. while and do-while behave much like in other languages, repeating a block as long as a condition holds true.

🏏

Cricket analogy: Like a bowler running through overs using a fixed sequence of deliveries (a range like 1..5) rather than manually counting balls one by one with an index variable, Kotlin's for loop iterates over ranges instead of Java's C-style index counting.

Syntax

kotlin
// for loop over a range
for (i in 1..5) {
    // uses i
}

// for loop over a collection
for (item in collection) {
    // uses item
}

// while loop
while (condition) {
    // repeats while condition is true
}

// do-while loop
do {
    // runs at least once
} while (condition)

Explanation

Kotlin's for loop always uses the in keyword to iterate: for (i in 1..5) iterates over a range, and for (item in list) iterates over any collection or array. There is no direct equivalent of Java's index-based for (int i=0; i<n; i++); ranges and step expressions cover that use case instead. while checks its condition before each iteration, so the body may run zero times, whereas do-while always executes its body at least once before checking the condition. break exits the nearest enclosing loop entirely, and continue skips to the next iteration. For nested loops, labels (e.g. outer@ for (...) { ... break@outer }) let break or continue target a specific outer loop instead of just the innermost one.

🏏

Cricket analogy: Like an umpire who checks the light meter before every over starts (while), versus one who always lets the first over of a session happen before checking conditions (do-while), and can call off just the current match without ending the whole tournament (break with a label targeting one match).

Example

kotlin
fun main() {
    // for loop with a range
    for (i in 1..5) {
        print("$i ")
    }
    println()

    // while loop
    var count = 0
    while (count < 3) {
        println("count = $count")
        count++
    }

    // labeled nested loop with break
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (j == 2) break@outer
            println("i=$i, j=$j")
        }
    }
}

// Output:
// 1 2 3 4 5
// count = 0
// count = 1
// count = 2
// i=1, j=1

Key Takeaways

  • Kotlin's for loop iterates using in over ranges, collections, or arrays - not C-style indexing.
  • while checks its condition before running the body, so it may execute zero times.
  • do-while always executes its body at least once, checking the condition afterward.
  • break exits the nearest loop, continue skips to the next iteration.
  • Labels (e.g. outer@) let break/continue target a specific loop in nested loop structures.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#LoopsInKotlinForWhileDoWhile#Loops#While#Syntax#Explanation#StudyNotes#SkillVeris