(Swift for Beginners)
Swift continue Statement
In this article, you will learn to use continue statement to transfer control of the program.
While you are working with loops or conditional statements, you may require to skip some statements inside the loop or terminate the loop immediately without checking the test expression.
In such cases, break and continue statements are used. Visit the page to learn more about break statement.
The continue statement stops the execution of statements inside a loop or a switch statement. It then skips back to evaluate the loop’s test expression again.
The syntax of a continue statement is:
continue
How continue statement works?
Example 1: Swift continue statement in for loop
for i in 1...5 {
if i == 4 {
continue
}
print("i = (i)")
}
When you run the program, the output will be:
i = 1 i = 2 i = 3 i = 5
In the above program, the sequence being iterated over is a range from 1 to 5.
The value of i is set to the first number in the range (1), and updated to the next number of the range on each iteration.
The loop also contains an if statement with expression i == 4
. The expression when evaluated to true (on 4th iteration) executes the continue
statement, skips the execution of statement print("i = (i)")
and moves to the next iteration (i = 5).
Example 2: Swift continue statement in while loop
var intialLevel:Int = -5, finalLevel:Int = 2
var currentLevel:Int = intialLevel
while currentLevel < finalLevel {
if intialLevel < 0 {
print("Starting level must be positive")
intialLevel = 0
currentLevel = intialLevel
continue //skips the current execution
}
currentLevel += 1
print("next level")
}
print("outside of while loop")
When you run the program,the output will be:
Starting level must be positive next level next level outside of while loop
In the above program, the while loop runs until currentLevel is less than finalLevel. Since, intialLevel is assigned value -5 and is less than 0, statements inside the if
executes.
When the program reaches continue
statement, it skips the statements of the while loop and jumps to check the condition currentLevel < finalLevel
again.
Example 3: Swift continue statement with nested loops
for j in 1...2 {
for i in 1...5 {
if i == 4 {
continue
}
print("i = (i)")
}
print("j = (j)")
}
When you run the program,the output will be:
i = 1 i = 2 i = 3 i = 5 j = 1 i = 1 i = 2 i = 3 i = 5
In the above program, continue
statement inside the if i == 4
only skips the execution of statement print("i = (i)")
inside the inner for loop. However, it doesn’t skip the execution of outer loop for i in 1...5
.
But, what if you want to continue
the outerloop as well. For that, you need to use labeled statements in Swift.
Labeled Statement with continue
Statements that have prefixes in the form (label : Statement) are called as labeled statement. The label is an identifier which you can refer later in the break or continue statements. To learn more about labeled statements, visit Swift labeled statement.
How labeled statement with continue works?
Here, label is an identifier. When continue
statement executes, the program skips the rest of the coe inside the loop and jumps to the statement where label starts.
Example 4: Labeled Statement with continue
outerloop: for j in 1...2 {
innerloop: for i in 1...5 {
if i == 4 {
continue outerloop
}
print("i = (i)")
}
print("j = (j)")
}
When you run the program, the output will be:
i = 1 i = 2 i = 3 i = 1 i = 2 i = 3
In the above program, there are two labeled statements outerloop: for j in 1...2
and innerloop: for i in 1...5
.
The label names outerloop and innerloop can be used with the continue
statement.
In the program, we have used the statement continue outerloop
to skip the execution of statements inside both inner and outer loops. In the process, values of both i and j are updated to the next number in the range.
Swift programming for Beginners – Swift continue Statement
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.