0Pricing
Swift Academy · Lesson

break, continue and Labeled Statements

Controlling loop flow with break, continue and labeled loops in Swift.

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

Welcome

`break` and `continue` give you fine-grained control over loop execution. Labeled statements extend this power to nested loops. Let's learn when each is appropriate.

break — Exit the Loop Early

`break` immediately terminates the current loop: ```swift for i in 1...10 { if i == 5 { break } print(i) } // 1, 2, 3, 4 ``` Execution continues with the first statement after the loop.

continue — Skip the Current Iteration

`continue` skips the rest of the current iteration and moves to the next: ```swift for i in 1...10 { if i % 2 == 0 { continue } print(i) } // 1, 3, 5, 7, 9 ``` Useful for skipping invalid or unneeded elements.

break in while Loops

`break` works the same in `while` loops: ```swift var n = 0 while true { n += 1 if n >= 5 { break } } print(n) // 5 ``` This is the idiomatic `while true / break` event-loop exit pattern.

break in switch — Explicit No-op

In Swift, an empty `switch` case is a compile error. Use `break` to explicitly do nothing: ```swift switch value { case .ignored: break // intentionally no action default: process(value) } ```

Labeled Loops

Label a loop with an identifier followed by a colon: ```swift outerLoop: for row in 0..<3 { for col in 0..<3 { if row == 1 && col == 1 { break outerLoop } print("(\(row),\(col))") } } ``` `break outerLoop` exits the *outer* loop entirely.

continue with Labels

Labels work with `continue` too, skipping to the next outer iteration: ```swift outerLoop: for row in 0..<3 { for col in 0..<3 { if col == 1 { continue outerLoop } print("(\(row),\(col))") } } // skips rest of inner loop when col reaches 1 ```

Combining where and break

Use a `where` clause to avoid explicit `break` when possible: ```swift // Less idiomatic: for n in 1...100 { if n > 10 { break } print(n) } // More idiomatic: for n in (1...100).prefix(10) { print(n) } ``` Prefer functional alternatives over imperative break when readable.

Early Return vs break

Inside a function, `return` exits both the loop and the function: ```swift func findFirst(_ target: Int, in array: [Int]) -> Int? { for element in array { if element == target { return element } } return nil } ``` Often cleaner than using `break` + tracking a result variable.

Avoiding break with first(where:)

Many manual break patterns can be replaced with collection methods: ```swift // break version: var found: Int? for n in numbers { if n > 10 { found = n; break } } // idiomatic: let found = numbers.first { $0 > 10 } ``` The functional version is shorter and expresses intent directly.

Quick Check

Which statement skips the **current** iteration and moves to the next?

Recap

Key takeaways: • `break` — exit the loop immediately • `continue` — skip to next iteration • Labeled loops (`label:`) let `break`/`continue` target an outer loop • Empty switch cases require an explicit `break` • Prefer functional alternatives (`first(where:)`, `prefix`) over manual break where possible Next: argument labels and parameter names in functions.

Frequently asked questions

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

Yes — the full text of “break, continue and 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 “break, continue and Labeled Statements”?

Controlling loop flow with break, continue and labeled loops in Swift. 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 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 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. for-in Loops with Ranges
  2. while and repeat-while Loops
  3. Iterating Collections with for-in
  4. break, continue and Labeled Statements
← Back to Swift Academy