Menu
Coddy logo textTech

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, 5

When 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, 6

The key difference: break stops the loop completely, while continue only skips to the next iteration.

challenge icon

Challenge

Easy
Write 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, use continue to 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, 5

When 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, 6

Key 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
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals