0Pricing
Swift Academy · Lesson

break and continue Basics

Exit or skip iterations cleanly.

break and continue Basics is a free Swift Academy lesson on CoddyKit — lesson 1 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 break Does

The break statement immediately exits the innermost loop, skipping any remaining iterations.

for n in 1...10 {
    if n == 4 {
        break
    }
    print(n)
}
print("done")

What continue Does

The continue statement skips the rest of the current iteration and jumps to the next one.

for n in 1...6 {
    if n % 2 == 0 {
        continue
    }
    print(n)
}

break vs continue

break ends the whole loop; continue only ends the current pass. Compare the two side by side.

for n in 1...5 {
    if n == 3 { continue }
    print("kept", n)
}

break in a while Loop

break works in any loop, including while.

var i = 0
while true {
    if i >= 3 {
        break
    }
    print(i)
    i += 1
}

continue Skips Work

Use continue to skip elements that fail a condition without nesting deeply.

let words = ["apple", "", "banana", "", "cherry"]
for w in words {
    if w.isEmpty {
        continue
    }
    print(w)
}

Early Exit on Found

A common pattern is to break as soon as you find what you are searching for.

let nums = [4, 8, 15, 16, 23, 42]
var found = -1
for n in nums {
    if n > 15 {
        found = n
        break
    }
}
print("first over 15:", found)

break in repeat-while

break also exits a repeat-while loop early.

var count = 0
repeat {
    count += 1
    if count == 2 { break }
} while count < 10
print(count)

Counting With continue

Combine a counter with continue to count only matching items.

var evens = 0
for n in 1...10 {
    if n % 2 != 0 { continue }
    evens += 1
}
print("even count:", evens)

break After Accumulating

Stop a running total once it crosses a threshold.

let costs = [10, 20, 30, 40]
var total = 0
for c in costs {
    total += c
    if total >= 50 { break }
}
print(total)

continue and continue Again

You can have multiple continue conditions to filter several cases.

for n in 1...10 {
    if n < 3 { continue }
    if n > 7 { continue }
    print(n)
}

Readability

Used well, break and continue flatten code and make loop intent clearer than deeply nested conditionals.

let scores = [40, 55, 70, 0, 90]
for s in scores {
    if s == 0 { continue }
    print("valid score:", s)
}

Quick Check

Test break and continue.

Recap

break exits the innermost loop entirely; continue skips to the next iteration. Both work in for, while, and repeat-while, and they help flatten and clarify loop logic.

for n in 1...5 {
    if n == 2 { continue }
    if n == 4 { break }
    print(n)
}

Frequently asked questions

Is the “break and continue Basics” lesson free?

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

Exit or skip iterations cleanly. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “break and continue Basics” 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