Continue
Part of the Fundamentals section of Coddy's Swift journey — lesson 46 of 86.
While break exits a loop entirely, sometimes you just want to skip the current iteration and move on to the next one. The continue statement does exactly that.
for number in 1...5 {
if number == 3 {
continue
}
print(number)
}
// Output: 1, 2, 4, 5When number equals 3, the continue statement skips the rest of that iteration. The loop doesn't stop—it just jumps to the next value. Notice that 3 is missing from the output, but 4 and 5 still print.
This is useful when you want to filter out certain values without stopping the entire loop. For example, printing only even numbers:
for number in 1...6 {
if number % 2 != 0 {
continue
}
print(number)
}
// Output: 2, 4, 6The key difference: break stops the loop completely, while continue only skips to the next iteration.
Challenge
EasyWrite a function sumExcludingMultiples that takes limit and skip and returns the sum of all numbers from 1 to limit, excluding any numbers that are multiples of skip.
Use a loop with continue to skip over multiples of the given number while accumulating the sum of all other values.
Logic:
- Loop through numbers from 1 to
limit - If the current number is a multiple of
skip, usecontinueto skip it - Otherwise, add the number to the running total
- Return the final sum
Parameters:
limit(Int): The upper bound of the range (inclusive)skip(Int): Numbers divisible by this value should be skipped
Returns: The sum of all numbers from 1 to limit that are not multiples of skip (Int)
For example, if limit is 10 and skip is 3, the function skips 3, 6, and 9, so it returns 1 + 2 + 4 + 5 + 7 + 8 + 10 = 37.
Cheat sheet
The continue statement skips the current iteration of a loop and moves to the next one, without exiting the loop entirely.
for number in 1...5 {
if number == 3 {
continue
}
print(number)
}
// Output: 1, 2, 4, 5When the condition is met, continue skips the remaining code in that iteration and jumps to the next value.
Example filtering even numbers:
for number in 1...6 {
if number % 2 != 0 {
continue
}
print(number)
}
// Output: 2, 4, 6Key difference: break exits the loop completely, while continue only skips to the next iteration.
Try it yourself
func sumExcludingMultiples(limit: Int, skip: Int) -> Int {
// Write code here
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input