Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A labeled continue is a control flow expression in Kotlin that terminates the current iteration of a specifically targeted enclosing loop and immediately proceeds to its next iteration. By binding a continue statement to a custom label, execution bypasses the default behavior of targeting the innermost loop, allowing precise control over nested iteration structures.

Syntax

A label is defined by an identifier followed immediately by the @ symbol, placed directly before the loop declaration. The jump is executed using the continue keyword appended with the @ symbol and the target identifier.
labelName@ for (item in collection) {
    for (innerItem in innerCollection) {
        if (condition) {
            continue@labelName
        }
    }
}

Mechanics and Execution Flow

  • Label Declaration: Any valid Kotlin identifier can be used as a label (e.g., outer@, loopA@). It must prefix a loop construct (for, while, or do-while).
  • Invocation: The invocation syntax (continue@labelName) must not contain whitespace between the continue keyword, the @ symbol, and the identifier.
  • Control Transfer: When the labeled continue is evaluated, the Kotlin compiler discards all remaining statements in the current iteration of both the innermost loop and any intermediate loops up to the targeted labeled loop.
  • Loop Evaluation:
    • In a for loop, execution jumps to the next step of the targeted loop’s iterator.
    • In while and do-while loops, execution jumps directly to the boolean condition evaluation of the targeted loop.

Structural Example

The following demonstrates the execution path when a labeled continue is invoked:
outer@ for (i in 1..3) {
    for (j in 1..3) {
        if (i == 2 && j == 2) {
            // Terminates the j loop for the current i iteration.
            // Control transfers to the iterator of the 'outer@' loop (i = 3).
            continue@outer 
        }
        println("i=$i, j=$j")
    }
}
Execution Output:
i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
i=3, j=1
i=3, j=2
i=3, j=3
In this structure, when i == 2 and j == 2, the continue@outer expression prevents i=2, j=2 and i=2, j=3 from executing. The inner loop is aborted, and the outer loop immediately advances to i = 3.
Master Kotlin with Deep Grasping Methodology!Learn More