Swift - Loops

 There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times. Following is the general from of a loop statement in most of the programming languages −

loops

Swift 4 programming language provides the following kinds of loop to handle looping requirements.


[1]. for-in

This loop performs a set of statements for each item in a range, sequence, collection, or progression.

Syntax

The syntax of a for-in loop in Swift 4 programming language is −

for index in var {
   statement(s)
}

For-In Loop

Example


var someInts:[Int] = [10, 20, 30]

for index in someInts {
   print( "Value of index is \(index)")
}

When the above code is executed, it produces the following result −

Value of index is 10
Value of index is 20
Value of index is 30


[2]. while loop

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

Syntax

The syntax of a while loop in Swift 4 programming language is −

while condition {
   statement(s)
}



Here statement(s) may be a single statement or a block of statements. The condition may be any expression. The loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop.

The number 0, the strings '0' and "", the empty list(), and undef are all false in a Boolean context and all other values are true. Negation of a true value by ! or not returns a special false value.

Flow Diagram

While Loops

The key point of a while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example

var index = 10

while index < 20 {
   print( "Value of index is \(index)")
   index = index + 1
}

Here we are using comparison operator < to compare the value of the variable index against 20. While the value of index is less than 20, the while loop continues executing a block of code next to it and as soon as the value of index becomes equal to 20, it comes out. When executed, the above code produces the following result −

Value of index is 10
Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 15
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19


[3]. repeat...while loop

Like a while statement, except that it tests the condition at the end of the loop body.

Syntax

The syntax of a repeat...while loop in Swift 4 is −

repeat {
   statement(s);
} 
while( condition );


It should be noted that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested. If the condition is true, the control flow jumps back up to repeat, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.

The number 0, the strings '0' and "", the empty list(), and undef are all false in a Boolean context and all other values are true. Negation of a true value by ! or not returns a special false value.

Flow Diagram

Repeat-While Loops

Example

var index = 10

repeat {
   print( "Value of index is \(index)")
   index = index + 1
}
while index < 20

When the above code is executed, it produces the following result −

Value of index is 10
Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 15
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19


[4]. continue statement

This statement tells a loop to stop what it is doing and start again at the beginning of the next iteration through the loop.

Syntax

The syntax for a continue statement in Swift 4 is as follows −

continue


Flow Diagram

Continue Statement

Example

var index = 10

repeat {
   index = index + 1
   if( index == 15 ){
      continue
   }
   print( "Value of index is \(index)")
} while index < 20

When the above code is compiled and executed, it produces the following result −

Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19
Value of index is 20


[5]. break statement

Terminates the loop statement and transfers execution to the statement immediately following the loop.

Syntax

The syntax for a break statement in Swift 4 is as follows −

break


Flow Diagram

Break Statement

Example

var index = 10

repeat {
   index = index + 1
   if( index == 15 ){
      break
   }
   print( "Value of index is \(index)")
} while index < 20

When the above code is compiled and executed, it produces the following result −

Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14


[6]. fallthrough statement

The fallthrough statement simulates the behavior of Swift 4 switch to C-style switch.

Syntax

The generic syntax of a switch statement in Swift 4 is as follows −

switch expression {
   case expression1 :
      statement(s)
      fallthrough /* optional */
   case expression2, expression3 :
      statement(s)
      fallthrough /* optional */

   default : /* Optional */
      statement(s);
}

If we do not use fallthrough statement, then the program will come out of the switch statement after executing the matching case statement. We will take the following two examples to make its functionality clear.


Example 1

The following example shows how to use a switch statement in Swift 4 programming without fallthrough −


var index = 10

switch index {
   case 100 :
      print( "Value of index is 100")
   case 10,15 :
      print( "Value of index is either 10 or 15")
   case 5 :
      print( "Value of index is 5")
   default :
      print( "default case")
}

When the above code is compiled and executed, it produces the following result −

Value of index is either 10 or 15


Example 2

The following example shows how to use a switch statement in Swift 4 programming with fallthrough −


var index = 10

switch index {
   case 100 :
      print( "Value of index is 100")
      fallthrough
   case 10,15 :
      print( "Value of index is either 10 or 15")
      fallthrough
   case 5 :
      print( "Value of index is 5")
   default :
      print( "default case")
}

When the above code is compiled and executed, it produces the following result −

Value of index is either 10 or 15
Value of index is 5



Comments

Popular posts from this blog

Java : Variables Declaring

Install DNF in RHEL/CentOS 7

SQL Self JOIN