Ios-Swift-Loop Statements
Swift 4 provides several control structures to allow more complicated execution paths.
In Swift 4 programming language, loops are used to execute a statement or a group of statements when you need to execute them multiple times.
Every programming language has some general form of loop statement.
A general structure of loop statement

Loops used in Swift 4 programming language
A list of most used loops in Swift 4 programming language:
for-in loop: The for-in loop is used to execute a set of statements for each item in a range, selection, collection etc.
while loop: The while loop is used to repeat a statement or a set of statements when the given condition is true. It checks the conditions before executing the loop body.
repeat... while loop: It is same as the while loop but it checks the condition at the end of the loop body.
To learn ios swift course visit:ios app development course
Swift For-in Loop
The Swift For-in loop is used to run a set of tasks for a certain number of times. This loop does repetitive process a number of times.
For example
If you want to write a syntax that prints "Hello JavaTpoint", the simple syntax will be:
- print("Hello JavaTpoint")
What would be if you have to write the same print statement 5 times? The simple solution would be:
- print("Hello JavaTpoint")
- print("Hello JavaTpoint")
- print("Hello JavaTpoint")
- print("Hello JavaTpoint")
- print("Hello JavaTpoint")
But, there is one solution. You can use for-in loop to print the same statement and make the code shorter.
- for i in 1...5 {
- //It will print Hello JavaTpoint for 5 times on the screen
- print("Hello JavaTpoint")
- }
Example 1:
- let names = ["Ajeet", "Aryan", "Ayan", "Alex"]
- for name in names {
- print("Good Morning \(name)")
- }
Output:
Good Morning Ajeet Good Morning Aryan Good Morning Ayan Good Morning Alex
Swift While and Repeat While Loop
While and Repeat while loops are used as an alternative of for-in loops when the number of iteration is not known. The while loop executes a set of statements until a false condition occur. This loop is generally used when you don't know about the number of iterations.
There are two types of loops in Swift:
- While loop
- Repeat While loop
Swift While Loop
The Swift while loop evaluates its condition at the start of each pass.
Syntax:
- while (TestExpression) {
- // statements
- }
Here, TestExpression is a Boolean expression. If it is true,
- Statements inside the while loop are executed.
- And, the TestExpressionis evaluated again.
This process goes on until the TestExpression evaluates false. When the TestExpression gets the false condition, the while loop is terminated.
Flow chart of While loop

Example:
- var currentLevel:Int = 0, finalLevel:Int = 6
- let gameCompleted = true
- while (currentLevel <= finalLevel) {
- //play game
- if gameCompleted {
- print("You have successfully completed level \(currentLevel)")
- currentLevel += 1
- }
- }
- //outside of while loop
- print("Terminated! You are out of the game ")
Output:
You have successfully completed level 0 You have successfully completed level 1 You have successfully completed level 2 You have successfully completed level 3 You have successfully completed level 4 You have successfully completed level 5 You have successfully completed level 6 Terminated! You are out of the game
In the above program, the while loop is executed until the condition is evaluated to false and as soon as it gets the false condition, it terminates.
For more information visit:swift course
Repeat While loop
The Repeat While loop is same as while loop but a difference that the body of repeat...while loop is executed once before the test expression is checked.
Syntax:
- repeat {
- // statements
- ...
- } while (testExpression)
In this loop, the body of repeat while loop is executed once and after that testExpression is checked.
Flowchart of Repeat While loop

Example:
- var currentLevel:Int = 0, finalLevel:Int = 5
- let gameCompleted = true
- repeat {
- //play game
- if gameCompleted {
- print("You have successfully completed level \(currentLevel)")
- currentLevel += 1
- }
- } while (currentLevel <= finalLevel)
- print("Terminated! outside of repeat while loop")
Output:
You have successfully completed level 0 You have successfully completed level 1 You have successfully completed level 2 You have successfully completed level 3 You have successfully completed level 4 You have successfully completed level 5 Terminated! outside of repeat while loop
To learn complete ios online course visit our OnlineItGuru blog:ios online course
Comments
Post a Comment