Blog / May 24, 2023 / 3 mins read / By Mahi Garg

Continue Statement in Swift

Loop control is a fundamental aspect of programming, allowing developers to iterate over collections and execute specific tasks. However, in real-world scenarios, not every iteration requires the same action. Swift provides a powerful tool called the continue statement, which allows you to skip specific iterations and continue to the next one within a loop. In this blog post, we’ll explore the continue statement in Swift, along with practical examples to demonstrate its usefulness.

Skipping Odd Numbers

Let’s consider a common scenario where we have an array of numbers, and we want to print only the even numbers while skipping the odd ones:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers {
    if number % 2 == 1 {
        continue // Skip odd numbers and proceed to the next iteration
    }
    print("Even number: \(number)")
}

In this example, the continue statement is used inside the loop to skip odd numbers (number % 2 == 1). When the condition is met, the loop immediately proceeds to the next iteration, effectively skipping the code below the continue statement.

Filtering String Elements

The continue statement is not limited to arrays of numbers; it can be used with any type of collection, including arrays of strings. Here’s an example where we want to filter out strings with a length greater than a certain value:

let fruits = ["apple", "orange", "banana", "kiwi", "pear", "mango"]

let maxLength = 5

for fruit in fruits {
    if fruit.count > maxLength {
        continue // Skip the string with a length greater than 'maxLength'
    }
    print("Fruit: \(fruit)")
}

In this case, the continue statement is employed to skip the strings with a length greater than maxLength. The loop continues with the next iteration when the condition is met, resulting in only shorter strings being printed.

Handling Optionals

The continue statement can be particularly useful when working with optionals. Consider an example where we have an array of optional integers, and we want to print only the non-nil values:

let optionalNumbers: [Int?] = [10, nil, 35, nil, 54, 78, nil, 22]

for num in optionalNumbers {
    guard let value = num else {
        continue // Skip nil values and proceed to the next iteration
    }
    print("Number: \(value)")
}

In this example, the guard statement is used to unwrap the optional num. If num is nil, the continue statement is executed, and the loop proceeds to the next iteration. If num contains a non-nil value, the unwrapped value is printed.

Skipping Specific Values

The continue statement can also be applied to skip specific values, not just based on conditions. Let’s say we have an array of colors, and we want to exclude the color “yellow” from our list:

let colors = ["red", "blue", "yellow", "green", "purple"]

for color in colors {
    if color == "yellow" {
        continue // Skip "yellow" and proceed to the next iteration
    }
    print("Color: \(color)")
}

In this example, the continue statement is used to skip the iteration when the color is equal to “yellow,” effectively excluding it from the output.

Conclusion

The continue statement is a powerful tool that enables developers to control the flow of their loops, skipping unnecessary iterations and improving the efficiency of their code. By using the continue statement judiciously, you can write more concise and optimized loops, enhancing the performance of your Swift applications.

Remember, with great power comes great responsibility, so make sure to use the continue statement wisely and keep your code readable and maintainable. Happy coding! 🚀

Comments