In Swift, the “continue” keyword is used in loops to skip over the current iteration of the loop and move on to the next iteration.
It can be particularly useful when you want to skip over certain elements in an array or when you want to move onto the next iteration of a loop without executing specific statements in that iteration.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for number in numbers { if number % 2 == 0 { continue } print(number) } /* Output 1 3 5 7 9 */
Quick notes
- In this code, the loop iterates over each element in the “numbers” array.
- If the current number is even, the “continue” keyword is executed and the loop moves on to the next iteration without executing the “print” statement.
- If the number is odd (in this case), the “print” statement is executed and the number is printed to the console.
- This example demonstrates how the “continue” keyword is used to skip a specific iteration of the loop when a certain condition is met.
In summary, the “continue” keyword in Swift is used to skip over a specific iteration of a loop and move on to the next iteration.