Swift continue Statement

The continue statement is used to skip the current iteration of the loop and the control flow of the program goes to the next iteration.

The syntax of the continue statement is:

continue

Before you learn about the continue statement, make sure you know about:


Working of Swift continue Statement

How continue statement works in Swift
Working of Swift continue Statement

Swift continue Statement With for Loop

We can use the continue statement with the for loop to skip the current iteration of the loop. Then the control of the program jumps to the next iteration. For example,

for i in 1...5 {
  
  if i == 3 {
    continue
  }
 
print(i)
}

Output

1
2
4
5

In the above example, we have used the for loop to print the value of i. Notice the use of the continue statement,

if i == 3 {
  continue
}

Here, when i is equal to 3, the continue statement is executed. Hence, the value 3 is not printed to the output.

Note: The continue statement is almost always used with decision-making statements.


continue with while loop

We can also skip the current iteration of the while loop using the continue statement. For example,

// program to print odd numbers from 1 to 10

var num = 0

while num <= 10{
  num += 1

  if (num % 2) == 0 {
    continue
}

print("\(num)")
}

Output

1
3
5
7
9

In the above example, we have used the while loop to print the odd numbers between 1 to 10. Notice the line,

if (num % 2) == 0 {
  continue
}

Here, when the number is even, the continue statement skips the current iteration and starts the next iteration.


Swift continue statement with nested loops

When we use the continue statement with nested loops, it skips the current iteration of the inner loop. For example,

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

Output

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

In the above example, we have used the continue statement inside the inner for loop.

if j == 2 {
  continue
}

Here, when the value of j is 2, the continue statement is executed. Hence, the value of j = 2 is never displayed in the output.


Swift Labeled continue

Till now, we have used the unlabeled continue statement. However, there is another form of continue statement in Swift known as labeled continue.

When using nested loops, we can skip the current iteration of the outer loop with a labeled continue statement.

How labeled continue works
Working of the Swift Labeled continue Statement

As we can see in the above image, we have used the outerloop identifier to specify the outer loop. Now, notice how the continue statement is used (continue outerloop)

Here, the continue statement is skipping the current iteration of the labeled statement (i.e. outer loop). Then, the program control goes to the next iteration of the labeled statement.

Example: Labeled Statement with continue

outerloop: for i in 1...3{
  
  innerloop: for j in 1...3 {
    
    if j == 3 {
      continue outerloop
    }
    
    print("i = \(i), j = \(j)")
  }
}

Output

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

In the above example, we have labeled our loops as:

outerloop: for i in 1...3 {...}

innerloop: for j in 1...3 {...}

This helps to identify the loops. Notice the use of labeled continue statement.

if j == 3 {
  continue outerloop
}

Here, the continue statement will skip the iteration of outer loop labeled with outerloop when the value of j is equal to 3.

Note: The use of labeled continue is often discouraged as it makes your code hard to understand.