Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Swift Continue statement
2.1.
Flow Diagram of continue statement
2.2.
Swift continue Statement With for Loop
2.3.
Swift continue with while loop
2.4.
Swift Labeled continue
3.
Swift Fallthrough Statement
3.1.
Syntax of Swift Fallthrough Statement
3.2.
Example of Fallthrough statement
4.
Frequently Asked Questions
4.1.
Can we use string value/variable in the switch test condition?
4.2.
Can we use continue instead of break to move the program’s execution at the starting of switch?
4.3.
Where fallthrough statements are used?
4.4.
What are the online websites for swift programming?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Swift continue and fallthrough statements

Author SAURABH ANAND
0 upvote

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 iOSOS 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.

Swift Fallthrough Statement

The fallthrough statement in Swift is used in the switch case to execute case statements that are adjacent to the matched case statements based on our criteria.

In general, switch-case statements in Swift will end execution when the first matching case statement is discovered, but if we wish to execute several case statements in switch case, we can use the fallthrough keyword to execute numerous case statements even if they do not match the defined condition.

Syntax of Swift Fallthrough Statement

The syntax for using a swift fallthrough statement in a switch case statement is as follows.

switch expression {
case pattern 1:
statements
fallthrough
case pattern 2:
statements
case pattern 3, pattern 4:
statements
fallthrough
default:
statements
}

 

If we look at the swift fallthrough statement syntax above, we'll notice that we used fallthrough in several switch-case statements to execute case statements that are adjacent to the matched switch-case statements.

Example of Fallthrough statement

The example below shows how to use fallthrough in a swift switch case statement to execute several case statements dependent on our needs.

var name = "ninjas"
switch(name) {
case "ninjas":
print("record matched")
fallthrough
case "two":
print("Not matched record but the fallthrough automatically picks next case statement")
default:
print("Default test case")
}

 

The following is a simple example of using fallthrough in a swift switch case statement to execute several case statements based on our requirements.

Output:

Record matched

 

Not matched record but the fallthrough automatically picks the next case statement

This is how we can use swift fallthrough statements to execute case statements next to matching case statements based on our needs.

Frequently Asked Questions

Can we use string value/variable in the switch test condition?

No, switch statements only operate with integral variables/literals such as integer, short, and character.

Can we use continue instead of break to move the program’s execution at the starting of switch?

No, the continue statement only works with looping statements.

Where fallthrough statements are used?

A fallthrough statement can be used only in a switch statement where the next statement to be run is a statement with a case or default label for that switch statement.

What are the online websites for swift programming?

Some of the available sites for online coding in swift language are Swiftstub.com, Runswiftlang.com, iswift.org, etc.

Conclusion

In this article, we have extensively discussed the concept of  Swift continue and fallthrough statements. We started with the introduction of  Swift continue and fallthrough statements, and separately discussed the concept of Swift continue and fallthrough statements with proper code and output statements.

After reading about the attributes of a file, are you not feeling excited to read/explore more articles on the topic of Swift? Don't worry; Coding Ninjas has you covered. To learn, see the Definitive Swift Tutorial for Beginnerswhy iOS & OS X Developers are choosing Swift?, and which is better to learn? iOS or Android?

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass