English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Swift break Statement

In this article, you will learn how to use the break statement to transfer control of the program.

When using loops or conditional statements, you may need to skip certain statements within the loop or immediately terminate the loop without checking the test expression

In this case, break and continue statements will be used. You will learn about them in the next chapter continue statement.

The break statement stops the execution of the loop or switch statement. Then, it jumps to the next statement after the loop or switch statement.

The syntax of the break statement is:

break

How does the break statement work?

Example1Swift loop control break statement

for i in 1...5 {
    if i == 4 {
        break
    }
    print("i = \(i)")
}
print("The end")

When the program is run, the output is:

i = 1
i = 2
i = 3
The end

In the above program, the range of the sequence to be iterated is1to5.

i's value is set to the range(1) of the first number, and it is updated to the next number in the range in each iteration.

The loop also contains an if statement, the expression is i==4. Expression is calculated as true (at the4statement is executed when the for-Iteration terminates. (At this

Then, it jumps outside the loop to print The End.

Example2:Swift break statement within the while loop

var currentLevel: Int = 1, finalLevel: Int = 2
var isLifeAvailable = true
while (isLifeAvailable) {
    
    if currentLevel > finalLevel {
        print("Game over, no levels left")
        break
    }
    //Play the game, enter the next level
    currentLevel += 1
    print("Next level")
}
print("Outside of the while loop")

When the program is run, the output is:

Next level
Next level
Game over, no levels left
Outside of the while loop

In the above program, the test expression of the while loop is always true.

When currentLevel is greater than finalLevel, the break statement within the if block is executed. Then, the program exits (terminates) the while loop and executes the statement after the loop, i.e., print("Outside of the while loop")..

Example3:Swift break statement with nested loops

for j in 1...2 {
    for i in 1...5 {
        if i == 4 {
            break
        }
        print("i = \(i)")
    }
    print("j = \(j)")
}

When the program is run, the output is:

i = 1
i = 2
i = 3
j == 1
i = 1
i = 2
i = 3
j == 2

In the above program, if i == 4 The break statement within only terminates for i in 1...5 execution of the inner loop. However, it continues to execute the outer loop for j in 1...2 .

But, if you also want to break the outer loop for j in 1...2What about that? For this, we use labelled statements in Swift.

Labelled (label) statement

Statements in the form of (label: Statement) are called labelled statements. A label is an identifier that you can refer to later in break or continue statements.

How do labelled statements with break work?

Here, label is an identifier. When the break execution statement is executed, it will terminate the loop labeled label within and the program will immediately jump to the statement after the labeled statement.

Example4:Labelled statements with break

outerloop: for j in 1...2{
    innerloop: for i in 1...5 {
        if i == 4 {
            break outerloop
        }
        print("i = \(i)")
    }
    print("j = \(j)")
}

When the program is run, the output is:

i = 1
i = 2
i = 3

In the above program, there are two labeled statements outerloop: for j in 1...2 and innerloop: for i in 1...5.

The label names outerloop and innerloop can be used with the break statement.

This statement break outerloop terminates two loops and ends the program.

If you are familiar with other programming languages, such as C, C ++such as Java, etc., then use the break statement to terminate the switch statement. However, in Swift, the switch statement completes its execution immediately after the first matching switch case is completed. Therefore, adding break in Swift switch cases is optional. For more information, please visitSwift switch statement.