0Pricing
Swift Academy · Lesson

Labeled Statements

Target outer loops with named labels.

Labeled Statements is a free Swift Academy lesson on CoddyKit — lesson 2 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 Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Label

A statement label names a loop so you can target it precisely with break or continue.

outer: for i in 1...3 {
    print("row", i)
}

Label Syntax

Write the label name followed by a colon directly before the loop keyword.

search: for n in 1...5 {
    print(n)
    if n == 3 { break search }
}

Naming Convention

Labels are ordinary identifiers. Choose descriptive names like outer, rowLoop, or search.

rowLoop: for r in 1...2 {
    print("processing row", r)
}

Labeled break

break label exits the loop with that label, not just the innermost one.

outer: for i in 1...3 {
    for j in 1...3 {
        if j == 2 { break outer }
        print(i, j)
    }
}

Labeled continue

continue label jumps to the next iteration of the labeled loop.

outer: for i in 1...3 {
    for j in 1...3 {
        if j == 2 { continue outer }
        print(i, j)
    }
}

Labels on while Loops

Any loop can be labeled, including while.

var i = 0
mainLoop: while i < 5 {
    i += 1
    if i == 3 { break mainLoop }
    print(i)
}

Why Labels Help

Without labels, break only exits the innermost loop. Labels let you control outer loops directly.

grid: for x in 0...2 {
    for y in 0...2 {
        if x + y == 3 { break grid }
        print(x, y)
    }
}

Multiple Labels

You can label several nested loops and target each independently.

rows: for r in 1...2 {
    cols: for c in 1...2 {
        if c == 2 { continue rows }
        print(r, c)
    }
}

Labels Clarify Intent

A well-named label documents which loop a jump affects, improving readability of nested code.

scan: for value in [3, 6, 9, 12] {
    if value > 8 { break scan }
    print(value)
}

Label Scope

A label is only visible within its loop. You cannot break to a label that is not enclosing the current statement.

first: for a in 1...2 {
    print("a", a)
    second: for b in 1...2 {
        if b == 2 { break first }
        print("b", b)
    }
}

Combining With Conditions

Labels pair naturally with conditions to implement search-and-stop logic across dimensions.

let matrix = [[1, 2], [3, 4]]
var target = -1
find: for row in matrix {
    for v in row {
        if v == 3 {
            target = v
            break find
        }
    }
}
print(target)

Quick Check

Test labeled statements.

Recap

A label (name: before a loop) lets break name and continue name target that specific loop rather than the innermost one. Labels are scoped to their loop and make nested loop control explicit.

outer: for i in 1...2 {
    for j in 1...2 {
        if j == 2 { continue outer }
        print(i, j)
    }
}

Frequently asked questions

Is the “Labeled Statements” lesson free?

Yes — the full text of “Labeled Statements” is free to read here on the web, and the Swift 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 Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Labeled Statements”?

Target outer loops with named labels. You practise Swift 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 Swift Academy?

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

How long does the “Labeled Statements” 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 Swift Academy lesson?

Yes. Every Swift 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. break and continue Basics
  2. Labeled Statements
  3. Breaking Out of Nested Loops
  4. Loop Control in switch Context
← Back to Swift Academy