In every programming language,Swift provides a variety of control flow statements in swift programming.Such as define which code gets executed, how many times it is executed and, conversely, which code gets by-passed when the program is executing. This is often referred to as control flow programming since it controls the flow of program execution.
Swift programming language has five control flow statements. What are they, and how can they be used code language?
1-The Swift for-in Statement
2-The repeat ... while loop
3-Breaking from Loops
4-The continue Statement
5-Using if ... else ... Statements
6-Using if ... else if ... Statements
The Swift for-in Statement:
The for-in loop is used to iterate over a sequence of items contained in a collection or number range and provides a simple to use looping option.
The syntax of the for-in loop is as follows in programming for loop.
for constant name in collection or range { // code to be executed }
In this syntax, constant name is the name to be used for a constant that will contain the current item from the collection or range through which the loop is iterating. The code in the body of the loop will typically use this constant name as a reference to the current item in the loop cycle.
Consider, using swift example, the following for-in loop construct:
for index in 1...5 { print("Value of index is \(index)") }
The loop begins by stating that the current item is to be assigned to a constant named index. The statement then declares a closed range operator to indicate that the for loop is to iterate through a range of numbers, starting at 1 and ending at 5. The body of the swift programming loop simply prints out a message to the console panel indicating the current value assigned to the index constant, resulting in the following output:
Value of index is 1 Value of index is 2 Value of index is 3 Value of index is 4 Value of index is 5
for-in loop is of particular benefit when working with collections such as arrays and dictionaries.
The declaration of a constant name in which to store a reference to the current item is not mandatory. In the event that a reference to the current item is not required in the body of the for loop, the constant name in the for loop declaration can be replaced by an underscore character. For swift coding example:
var count = 0 for _ in 1...5 { // No reference to the current value is required. count += 1 }
The while Loop:-
The Swift for loop described previously works well when it is known in advance swift how many times a particular task needs to be repeated in a program. There will, however, be instances where code needs to be repeated until a certain condition is met, with no way of knowing in advance how many repetitions are going to be needed to meet that criteria. To address this need, Swift programming provides the while loop.
Essentially, the while loop repeats a set of tasks while a specified condition is met. The while loop syntax is defined as follows:
while condition { // Swift statements go here }
In the above syntax, condition is an expression that will return either true or false and the Swift statements go here comment represents the code to be executed while the condition expression is true. For iOS swift example.
var myCount = 0 while myCount < 100 { myCount += 1 }
In the above example, the while expression will evaluate whether the myCount variable is less than 100. If it is already greater than 100, the code in the braces is skipped and the loop exits without performing any tasks.
If, on the other hand, myCount is not greater than 100 the code in the braces is executed and the loop returns to the while statement and repeats the evaluation of myCount. This process repeats until the value of myCount is greater than 100, at which point the loop exits.
The repeat ... while loop:-
The while loop evaluates an expression before executing the code contained in the body of the loop. If the expression evaluates to false on the first check then the code is not executed.
The repeat ... while loop, on the other hand, is provided for situations where you know that the code contained in the body of the loop will always need to be executed at least once.
For example-
var i = 10 repeat {i -= 1 } while (i > 0)
Breaking from Loops:
Having created a loop, it is possible that under certain conditions you might want to break out of the loop before the completion criteria have been met (particularly if you have created an infinite loop).
var j = 10 for _ in 0 ..< 100 { j += j if j > 100 { break } print("j = \(j)") }
In the above example the loop will continue to execute until the value of j exceeds 100 at which point the loop will exit and execution will continue with the next line of code after the loop.
The continue Statement:
The continue statement causes all remaining code statements in a loop to be skipped, and execution to be returned to the top of the loop. In the following example, the print function is only called when the value of variable i is an even number:
var i = 1 while i < 20 { i += 1 if (i % 2) != 0 { continue } print("i = \(i)") } while conditional expression }
The continue statement in the above example will cause the print call to be skipped unless the value of i can be divided by 2 with no remainder.
If Statement:
The if statement is perhaps the most basic of control flow options available to the Swift programming
who are familiar with C, Objective-C, C++ or Java will immediately be comfortable using Swift if statements.
The basic syntax of the Swift if statement is as follows.
if boolean expression { // Swift code to be performed when expression evaluates to true }
For example, if a decision needs to be made depending on whether one value is greater than another, we would write code similar to the following:
let x = 10 if x > 9 { print("x is greater than 9!") }
Clearly, x is indeed greater than 9 causing the message to appear in the console panel.
Using if ... else ... Statements:
The next variation of the if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false. The syntax for this construct is as follows.
if boolean expression { // Code to be executed if expression is true } else { // Code to be executed if expression is false }
Using the above syntax, we can now extend our previous example to display a different message if the comparison expression evaluates to be false.
let x = 10 if x > 9 {print("x is greater than 9!") } else { print("x is less than 9!") }
Clearly, x is indeed greater than 9 causing the message to appear in the console panel.
Using if ... else ... Statements:
The next variation of the if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false. The syntax for this construct is as follows:
if boolean expression { // Code to be executed if expression is true } else { // Code to be executed if expression is false }
Using the above syntax, we can now extend our previous example to display a different message if the comparison expression evaluates to be false.
let x = 10 if x > 9 {print("x is greater than 9!") } else { print("x is less than 9!") }
In this case, the second print statement would execute if the value of x was less than 9.
Using if ... else if ... Statements:
So far we have looked at if statements which make decisions based on the result of a single logical expression. Sometimes it becomes necessary to make decisions based on a number of different criteria. For this purpose, we can use the if ... else if ... construct, an example of which is as follows:
let x = 9; if x == 10 { print("x is 10") } else if x == 9 { print("x is 9") } else if x == 8 { print("x is 8") }
-------------------------------------
Thanks for reading and happy coding. 🙂
- How to remove duplicate elements from an array with Swift 5
- How can I write text in multi-lines in Swift?
- How to store Model Class data in User Defaults in Swift?
- Swift Tutorial : Don't forget the heart of Swift Structures and Enumerations With Examples
- Swift Error Handling Best Practices With Xcode Example
- Class Extension VS. Subclassing in Swift?
- Swift Tutorial : Is Swift Object- Oriented Programming?
- Getters and Setters in swift | How to work willSet and didiSet? | Stored property | Computed property | Property Observers
0 Comments