0Pricing
Kotlin Academy · Lesson

break, continue, and Labeled Returns

Control loop flow with break/continue and escape nested loops with labels.

break, continue, and Labeled Returns is a free Kotlin Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Loop Flow Control

Kotlin supports break (exit the loop), continue (skip to next iteration), and labels for breaking out of nested loops.

break Basics

break exits the innermost enclosing loop immediately.

fun main() {
    for (i in 1..10) {
        if (i == 5) break
        print("$i ")
    }
    println() // 1 2 3 4
}

continue Basics

continue skips the rest of the current iteration and moves to the next one.

fun main() {
    for (i in 1..10) {
        if (i % 2 == 0) continue
        print("$i ")
    }
    println() // 1 3 5 7 9
}

Labeled Loops

Tag a loop with label@ and reference it from break@label or continue@label to control outer loops from inside nested ones.

fun main() {
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (i == 2 && j == 2) break@outer
            println("$i,$j")
        }
    }
}

continue with a Label

Use continue@outer to skip to the next iteration of an outer loop.

fun main() {
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (j == 2) continue@outer
            println("i=$i j=$j")
        }
    }
}

return@lambda

Inside forEach and other inline functions, plain return exits the enclosing function. Use return@forEach to exit only the lambda.

fun main() {
    listOf(1, 2, 3, 4, 5).forEach {
        if (it == 3) return@forEach
        println(it)
    }
    println("Done")
}

Non-Local return in Lambdas

If the lambda is inline (like forEach), a plain return exits the entire enclosing function — sometimes surprising.

fun process() {
    listOf(1, 2, 3).forEach {
        if (it == 2) return // exits process()
        println(it)
    }
    println("never printed")
}
fun main() { process() }

Custom Label on Lambda

You can label a lambda explicitly with name@ for a clearer return target.

fun main() {
    listOf(1, 2, 3, 4).forEach loop@{
        if (it == 3) return@loop
        println(it)
    }
}

break in while

break and continue work the same in while and do-while.

fun main() {
    var i = 0
    while (true) {
        if (i >= 3) break
        println("i = $i")
        i++
    }
}

Combining Label and Continue

Labels and continue let you skip whole iterations of nested algorithms without flag variables.

fun main() {
    val grid = listOf(listOf(1,2,0), listOf(3,4,5), listOf(0,6,7))
    rows@ for (row in grid) {
        for (cell in row) {
            if (cell == 0) continue@rows
            print("$cell ")
        }
        println()
    }
}

When Not to Use Labels

Labels add complexity. If you find yourself reaching for them often, consider extracting an inner loop into a function with a regular return.

fun firstMatch(grid: List<List<Int>>, target: Int): Pair<Int,Int>? {
    for (r in grid.indices) for (c in grid[r].indices) {
        if (grid[r][c] == target) return r to c
    }
    return null
}
fun main() {
    val g = listOf(listOf(1,2),listOf(3,4))
    println(firstMatch(g, 3))
}

Quick Check

How do you exit the outer loop from inside an inner loop in Kotlin?

Recap

break exits the innermost loop; continue skips to the next iteration. Use label@ + break@label/continue@label for nested control. In lambdas, prefer return@forEach to return to avoid non-local exits.

Frequently asked questions

Is the “break, continue, and Labeled Returns” lesson free?

Yes — the full text of “break, continue, and Labeled Returns” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “break, continue, and Labeled Returns”?

Control loop flow with break/continue and escape nested loops with labels. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “break, continue, and Labeled Returns” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. for Loop Over Ranges and Collections
  2. while and do-while: When to Use Each
  3. forEach, repeat, and forEachIndexed
  4. break, continue, and Labeled Returns
← Back to Kotlin Academy