Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Conditional Expressions and Loops  ›  Capstone Project

break, continue and Labels

break & continue 15 minbreak leaves the innermost loop, continue skips to its next round, and a label such as outer@ lets either one target an enclosing loop instead.

break, continue and Labels

A customer walks in and asks for a table. You scan the café floor row by row, table by table. The moment you see a free one, you stop looking. And if a whole row is roped off for cleaning, you skip it without checking a single table. Loops need the same two moves.

The rule

break leaves the loop; continue skips this round; a label picks which loop.

val tables = listOf("taken", "taken", "free", "free")
for ((i, t) in tables.withIndex()) {
    if (t == "taken") continue     // skip to the next table
    println("Table $i is free")
    break                          // stop searching
}
// prints: Table 2 is free
  • break ends the innermost loop immediately. Code after the loop carries on.
  • continue abandons the rest of the current round and jumps to the next one. In a while loop, that means back to the condition.

With nested loops, both words affect only the innermost loop. To reach an outer loop, give it a label: a name followed by @, placed before the loop. Then write break@name or continue@name.

val floor = listOf(
    listOf("taken", "taken"),
    listOf("taken", "free"),
    listOf("free", "free")
)
rows@ for ((r, row) in floor.withIndex()) {
    for ((c, t) in row.withIndex()) {
        if (t == "free") {
            println("Seat at row $r, table $c")
            break@rows             // leave both loops
        }
    }
}
// prints: Seat at row 1, table 1

A plain break there would leave only the inner loop. The outer loop would move on to row 2 and print another seat.

continue@rows would abandon the current row and jump to the next one, which is how you skip a closed row from inside the inner loop.

The shortcut

At every break or continue, draw an arrow to the loop it belongs to: the nearest enclosing loop, unless a label names another. break lands just after that loop; continue lands at its next round.

Tracing the labelled searchStep 1 of 4
  1. Row 0

    Both tables are taken. The inner loop ends normally; the outer moves on.

Labels can make code harder to follow. If you need them often, moving the search into its own function and using return is usually clearer.

Drills

Answer fast — trust the rule.

quick drill

What does for (i in 1..5) { if (i == 3) break; print(i) } print?

quick drill

What does for (i in 1..5) { if (i == 3) continue; print(i) } print?

quick drill

Inside two nested loops, a plain break in the inner loop does what?

quick drill

Which line correctly labels a loop and later leaves it?

quick drill

outer@ for (i in 1..3) { for (j in 1..3) { if (j == 2) continue@outer; print("[$i$j]") } }. What is printed?

Remember

  • break leaves the loop; continue jumps to its next round.
  • Both target the innermost loop unless a label (name@) says otherwise.
  • Draw the arrow: break lands after that loop, continue at its next round.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFinding a free table

The café floor is a grid: rows of tables, each marked "free" or "taken". Write nested for loops that find the first free table and stop searching completely, and explain why a plain break is not enough. Also show how to skip rows marked as closed using continue.