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

Swift continue Statement

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

When using loops or conditional statements, it may be necessary to skip certain statements within the loop or terminate the loop immediately without checking the test expression.

In this case, break and continue statements will be used. Visit this page to learn more aboutbreak statement'sMore information.

The continue statement stops the execution of the loop or the statements within the switch statement. Then, it jumps back to re-evaluate the test expression of the loop.

The syntax of the continue statement is:

continue

How does the continue statement work?

Example1:Swift continue statement in the for loop

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

When running this program, the output is:

i = 1
i = 2
i = 3
i = 5

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

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

The loop also contains the expression i = = 4The if statement. When the expression evaluates to true (at the4In the next iteration, execute the continue statement to skip the execution of the statement print("i = \(i)" and go to the next iteration (i = 5)。

Example2Swift continue statement in the while loop

var intialLevel:Int = -5, finalLevel:Int = 2
var currentLevel:Int = intialLevel
while currentLevel < finalLevel {
    if intialLevel < 0 {
        print("The starting level must be a positive number")
        intialLevel = 0
        currentLevel = intialLevel
        continue //Skip the current execution
    }
    currentLevel += 1
    print("next level")
}
print("Outside the while loop")

When running this program, the output is:

The starting level must be a positive number
next level
next level
Outside the while loop

In the above program, the while loop runs until currentLevel is less than finalLevel. Since the value assigned to intialLevel is-5and less than 0, so the internal statements in if are executed.

When the program reaches the continue statement, it skips the statements in the while loop and then jumps back to check the condition currentLevel<finalLevel again.

Example3Swift Continue statement with nested loops

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

When running this program, the output is:

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

In the above program, if i==4The continue statement in it only skips the execution of the statement print("i=\(i)" in the internal for loop. However, it does not skip for i in 1...5 Execution of the external loop.

But what if you also want to continue the outer loop? For this, you need to use labeled statements in Swift.

Declaration with continue label

A statement with a prefix in the form (label: Statement) is called a labeled statement. A label is an identifier that you can refer to later in break or continue statements.

How does a label with continue work?

Here, label is an identifier. When the continue statement is executed, the program skips the rest of the code in the loop and jumps to the statement starting with label.

Example4:Statements with continue Label

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

When running this program, the output is:

i = 1
i = 2
i = 3
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 continue statement.

In the program, we used the continue outerloop statement to skip the execution of statements in both the outer and inner loops. During this process, the values of i and j will be updated to the next number in the range.