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:
  1. print("Hello JavaTpoint")
What would be if you have to write the same print statement 5 times? The simple solution would be:
  1. print("Hello JavaTpoint")
  2. print("Hello JavaTpoint")
  3. print("Hello JavaTpoint")
  4. print("Hello JavaTpoint")
  5. print("Hello JavaTpoint")
But, there is one solution. You can use for-in loop to print the same statement and make the code shorter.
  1. for i in 1...5 {
  2. //It will print Hello JavaTpoint for 5 times on the screen
  3. print("Hello JavaTpoint")
  4. }

Example 1:

  1. let names = ["Ajeet", "Aryan", "Ayan", "Alex"]
  2. for name in names {
  3. print("Good Morning \(name)")
  4. }
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:
  1. While loop
  2. Repeat While loop

Swift While Loop

The Swift while loop evaluates its condition at the start of each pass.

Syntax:

  1. while (TestExpression) {
  2. // statements
  3. }
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:

  1. var currentLevel:Int = 0, finalLevel:Int = 6
  2. let gameCompleted = true
  3. while (currentLevel <= finalLevel) {
  4. //play game
  5. if gameCompleted {
  6. print("You have successfully completed level \(currentLevel)")
  7. currentLevel += 1
  8. }
  9. }
  10. //outside of while loop
  11. 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:
  1. repeat {
  2. // statements
  3. ...
  4. } 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:

  1. var currentLevel:Int = 0, finalLevel:Int = 5
  2. let gameCompleted = true
  3. repeat {
  4. //play game
  5. if gameCompleted {
  6. print("You have successfully completed level \(currentLevel)")
  7. currentLevel += 1
  8. }
  9. } while (currentLevel <= finalLevel)
  10. 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

Popular posts from this blog

what is swift Guard?and explain Ios Swift Guard Statements

How to Reuse SwiftUI Views with LibraryContentProvider and Swift Package

iOS UI Segmented Controls