Introduction
Before learning about Swift continue and fallthrough statements, let us first see what is swift.
Swift is a multi-paradigm, compiled programming language for designing apps for iOS, OS X, tvOS, and watchOS. Apple Inc. is the company behind it. It is a powerful and intuitive language that is simple to pick up. Swift code is secure, precise, and speedy.
Swift is based on the Objective-C runtime library, allowing C, Objective-C, C++, and Swift code to run in the same application. Swift is an open-source programming language included in Xcode since version 6.
Swift logo
Source: https://www.stickpng.com/img/icons-logos-emojis/tech-companies/swift-logo
Swift 4 is the most recent version of Swift, which includes all of the capabilities of Swift 3 while additionally improving the language's robustness and stability. Swift 4 has updated the standard library, added new capabilities like smart key paths and serialization, and reduced build times and app size.
This blog will talk about the Swift continue and fallthrough statements. Check out our blog on the definitive Swift tutorial for beginners to learn more about Swift.
Swift Continue statement
The continue statement skips the current iteration of the loop and directs the program's control flow to the next iteration. In Swift 4, the continue statement instructs a loop to halt what it is doing and restart at the beginning of the following iteration through the loop.
The syntax of the continue statement is:
continue
Flow Diagram of continue statement
Here is the flow diagram for controls of the continue statement.
Flow diagram of continue statement
Source: https://www.tutorialspoint.com/swift/swift_continue_statement.htm
Swift continue Statement With for Loop
To skip the current iteration of the loop, we can use the continue statement with the for loop. The program then advances to the next iteration.
For example:
for i in 1...5 {
if i == 3 {
continue
}
print(i)
}
Output:
1
2
4
5
The continue statement is executed when i equals 3. As a result, the value 3 is not printed in the output.
Swift continue with while loop
Using the continue command, we can also skip the current iteration of the while loop.
For example:
// print odd numbers from 1 to 10
var x = 0
while x < 10{
x += 1
if (x % 2) == 0 {
continue
}
print("\(x)")
}
Output:
1
3
5
7
9
When a number is an even(x%2 == 0), the continue statement skips the current iteration and begins the next iteration.
Swift Labeled continue
We've been using the unlabeled continue statement up until now. In Swift, there is another type of continue statement known as labeled continue. When using nested loops, we may use a labeled continue statement to skip the current iteration of the outer loop.
outerloop: for x in 1...3 {
innerloop: for y in 1...3 {
if y == 3 {
continue outerloop
}
print("x = \(x), y = \(y)")
}
}
Output:
x = 1, y = 1
x = 1, y = 2
x = 2, y = 1
x = 2, y = 2
x = 3, y = 1
x = 3, y = 2
Here, the continue statement will skip the loop of the outer loop labeled with the outer loop when the value of y is equal to 3.
Note: The usage of labeled continue is frequently discouraged since it makes your code difficult to understand.