break and continue Basics
Exit or skip iterations cleanly.
break and continue Basics is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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)
}Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “break and continue Basics” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “break and continue Basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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
- break and continue Basics
- Labeled Statements
- Breaking Out of Nested Loops
- Loop Control in switch Context