Swift Continue Statement: In this tutorial, we will learn about the continue statement with the assistance of examples.

Swift Continue Statement

In Swift, the continue statement gives you an approach to skip the current iteration of any loop. At the point when a continue statement is experienced in the loop, the remainder of the statements insider loop body for the current iteration and returns the program execution to the absolute first statement on the loop body. It doesn’t end the loop rather continues with the next iteration.

The continue statement is used to avoid 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, ensure you think about:


Swift continue Statement With for Loop

We can use the continue statement with the for loop to avoid the current emphasis of the loop. At that point, the control of the program leaps to the next emphasis. For instance,

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 worth of I. Notice the use of the continue statement,

if i == 3 {
  continue
}

Here, when I is equivalent to 3, the continue statement is executed. Thus, the worth 3 isn’t printed to the output.

Note: The continue statement is quite often used with decision-making statements.


continue with the while loop

We can likewise avoid the current iteration of the while loop using the continue statement. For instance,

// 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

At the point when we use the continue statements with nested loops, it skips the current iteration of the inner loop. For instance,

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 worth of j is 2, the continue statement is executed. Subsequently, the value of j = 2 is never shown in the output.


Swift Labeled continue

Till now, we have used the unlabeled continue statement. Be that as it may, there is another type of continue statement in Swift known as labeled continue.

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

As we can see in the above picture, we have used the outerloop identifier to indicate the outer loop. Presently, notice how the continue statement is used (continue outerloop)

Here, the continue statement is avoiding the current iteration of the labeled statement (for example outer loop). At that point, 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 = 2, 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 assists with distinguishing the loops. Notice the use of labeled continue statement.

if j == 3 {
  continue outerloop
}

Here, the continue statement will avoid the iteration of outer loop labeled with outerloop when the worth of j is equivalent to 3.

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


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.