In the previous chapters, you have seen that the conditions can be applied to a program to branch the execution of statements within a program. But there may arise some situations where programmers have to repeat the same block of the statement(s) several times. For that, Swift includes the usual collection of loops and conditional execution features. Swift provides a standard loop construct such as for, while, do while loop variants.



What is Looping in Swift?

Looping, also known iteration is used in a program to repeat a specific block/segment of code(s). This reduces the tasks of writing the same thing again and again. Here is the flowchart showing how loop statements work:

How Loop Works

There are various types of loops. These are:

for-condition-increment loops

The for-condition-increment loop is functionally the same as the 'for' loop in C. The loop consists of an initialization phase, a test, an increment, and a set of statements that are executed for every iteration of the loop.

Syntax:

for initialization; condition; increment {
statements
}

It has three phases. The initialization phase set up the condition for the start of the loop, i.e., the initialization of the loop counter. The second part tests the condition that has been met, whenever this evaluates to true, the statements in the body of the loop are executed once. 3rdly, the incremented phase adjusts some variable or value that forms part of the condition test to ensure a stopping condition can be reached (typically, incrementing the loop counter). It is to be noted that semicolons separate all of the three expressions.

for-in loops

You use the for-in loop to iterate over sequences or collections of things, such as the elements of an array or dictionary, the characters in a string, or a range of numbers. Here's the general form is:

Syntax:

for index in sequence {
statements
}

In the above example, this iterates over a range of numbers, the loop index variable (i) takes on the value of the next number in the range each time through the loop:

for i in 3...6 {
print (i)
}

while loops

While loops test the condition ahead of the loop's body and only if the condition evaluates to true, then the loop body gets executed. The general format is as follows:

Syntax:

while condition {
statements
}

Programmers can use the while loop to replicate the functionality of the for-condition-increment loop, as follows:

Example:

var count = 0
while (count < 8) {
print (count)
count ++
}

In this looping statement, the condition is tested before executing the body of the loop. If the condition evaluates to false for the first time it is executed, the statements in the body of the loop will never execute.

Repeat-While / Do - while Loop

Repeat-while loops test the termination condition at the end of the loop, rather than at the start. This means the statements in the body of the loop are guaranteed to be executed at least once. Loop execution continues until the condition evaluates to false. The general format for a repeat-while loop looks like this:

Syntax:

repeat {
statements
} while condition

Here's an example of repeat while loop:

Example:

var t = 0
repeat {
print (t)
t++
} while (t < 10)

It is to be noted that the repeat-while loop in Swift 2.0 replaces the do-while

loop from Swift 1.0.

Jump Statements of Swift

Continue Statement

The continue statement in Swift instructs the loop in which it is for stopping what it is doing and start again at the beginning of the next iteration through the loop.

Example:

import Cocoa
var value = 6
do{
   value = value + 1    
   if( value == 12 ){
      continue
   }
   println( "Value is \(value)")
}while value < 15

Break Statement

The break statement is used to encounter inside a loop. The loop gets terminated at once, and the program control resumes at the next statement following the loop.

Example:

import Cocoa
var value = 6
do{
   value = value + 1
   if( value == 12 ){
      break
   }
   println( "Value is \(value)")
}while value < 15


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram