Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SwiftBeginner - About break and continue Statements

About break and continue Statements - Images: Japanese

Hey there, everyone! Next up, let's go over the 'break statement' and the 'continue statement'.

First, let's start with the basic usage of the 'break statement'. The 'break statement' made a brief appearance back in the switch statement article.

The 'break statement' is a construct that lets you "exit a 'switch statement', a loop, or similar." The syntax is simple — just write break wherever you want to stop execution.

Let's take a look at a sample below that uses a 'break statement' inside a 'switch statement'.

let n = 1

switch n {
    case 1:
        break // The 'switch statement' ends here.
        print("It's 1.") // This line is never reached because 'break' came first.

    default:
        print("It's the default.")
}

Focus on the 'break' line above. There's a print("It's 1.") call right below it, but since execution hits 'break' first, the whole block exits without printing anything.

Now let's try using 'break' inside a 'for-in statement'. Take a look at the sample below — we'll combine it with an 'if statement' for a bit of practice.

for i in 0...10 { // Loops 11 times.
    if i >= 3 { // Runs when 'i' is 3 or greater.
        break
    }

    print(i)
}

Take a look at the 'if statement' above. The condition is i >= 3, so when the value of 'i' reaches 3 or more, 'break' fires and the loop ends.

Running this gives the following output.

0
1
2

Even though the loop was supposed to run 11 times, it stopped as soon as 'i' hit 3. That's what the 'break statement' does. The same approach works with 'while statements' and 'repeat-while statements' too.

var n = 0

while true {
    if n >= 3 {
        break
    }

    print(n)
    n = n + 1
}

var _n = 0

repeat {
    if _n >= 3 {
        break
    }

    print(_n)
    _n = _n + 1
} while true

Notice that both the 'while statement' and the 'repeat-while statement' above have true hardcoded as their condition — which would normally cause an infinite loop. But by using a 'break statement', we can exit the loop when we want. This is a very common pattern, so it's worth keeping in mind.

That covers the break statement.

Next up is the continue statement.

The 'continue statement' is a construct that lets you "jump to the top of the next loop iteration." Just like 'break', you simply write continue where you need it.

That might sound a bit abstract, so let's check out the sample below.

for i in 0...10 { // Loops 11 times.
    if i % 3 == 0 {
        continue
    }

    print(i)
}

In this example, the 'continue statement' runs whenever 'i' is evenly divisible by 3. In other words, "when 'i' is divisible by 3, that iteration is skipped and we move on to the next one." So the output looks like this.

1
2
4
5
7
8
10

3, 6, and 9 are all skipped. That's the 'continue statement' in action.

The same usage applies to while statements and repeat-while statements.

var n = 0

while n <= 10 {
    n = n + 1

    if n % 3 == 0 {
        continue
    }

    print(n)
}

var _n = 0

repeat {
    _n = _n + 1

    if _n % 3 == 0 {
        continue
    }

    print(_n)
} while _n <= 10

One thing to watch out for here: when writing code like the above, pay close attention to where you put the update operation. Writing it like this will send you straight into an infinite loop.

// These cause infinite loops — do not run.
var n = 0

while n <= 10 {
    if n % 3 == 0 {
        continue
    }

    print(n)

    n = n + 1
}

var _n = 0

repeat {
    if _n % 3 == 0 {
        continue
    }

    print(_n)

    _n = _n + 1
} while _n <= 10

The reason is that continue causes execution to jump to the next iteration, bypassing everything below it.

The update operations n = n + 1 and _n = _n + 1 are written below the 'continue' lines. So when 'continue' fires, the update never happens — and you're stuck in an infinite loop.

// These cause infinite loops — do not run.
var n = 0

while n <= 10 {
    if n % 3 == 0 {
        continue // When 'continue' runs, the 'n = n + 1' operation is skipped, so 'n' never updates and the loop runs forever.
    }

    print(n)

    n = n + 1
}

var _n = 0

repeat {
    if _n % 3 == 0 {
        continue // When 'continue' runs, the '_n = _n + 1' operation is skipped, so '_n' never updates and the loop runs forever.
    }

    print(_n)

    _n = _n + 1
} while _n <= 10

This is an easy mistake to make, so watch out for it.

One more thing to keep in mind: the "start of the next loop iteration" means the very top of the loop body. Make sure you don't mix that up.

for i in 0...10 { // Loops 11 times.
// The next iteration starts around here.
    if i % 3 == 0 {
        continue
    }

    print(i)
}

Also, since the continue statement means "move on to the next loop iteration," it can only be used inside a loop. Keep that in mind too.

if true {
    continue // Error.
}

And that wraps up the 'break statement' and the 'continue statement'. In the next article, we'll be covering 'classes' and 'instances'. See you there!

Take care!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .