What is the use of continue statement in Swift loop?

The continue statement is used in Swift loop to change execution from its normal sequence. It stops currently executing statement and starts again at the beginning of the next iteration through the loop.

In Swift, the continue statement is used within loops (such as for, while, or repeat-while loops) to skip the remaining code within the current iteration of the loop and move on to the next iteration. Essentially, it allows you to prematurely end the current iteration and start the next one without executing the remaining code within the loop’s body for that particular iteration.

Here’s a simple example to illustrate its usage:

for i in 1...5 {
if i == 3 {
continue // Skips the rest of the loop's body for i == 3
}
print(i)
}

Output:

1
2
4
5

In this example, when i is equal to 3, the continue statement is encountered, causing the loop to skip the print(i) statement and move on to the next iteration. As a result, the number 3 is not printed, and the loop continues with the next value.