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 −
Swift programming language provides the following kinds of loop to handle looping requirements. Click the following links to check their detail.
Loop Type | Description |
---|---|
for-in |
This loop performs a set of statements for each item in a range, sequence, collection, or progression.
The for-in loop iterates over collections of items, such as ranges of numbers, items in an array, or characters in a string −
Syntax
The syntax of a for-in loop in Swift programming language is −
for index in var { statement(s) } Flow DiagramExampleimport Cocoa var someInts:[Int] = [10, 20, 30] for index in someInts { println( "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 |
for loop |
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax
The syntax of for loop in Swift programming language is as follows −
for init; condition; increment{ statement(s) }
The flow of control in a for loop is as follows −
Flow DiagramExampleimport Cocoa var someInts:[Int] = [10, 20, 30] for var index = 0; index < 3; ++index { println( "Value of someInts[\(index)] is \(someInts[index])") }
When the above code is executed, it produces the following result −
Value of someInts[0] is 10 Value of someInts[1] is 20 Value of someInts[2] is 30 |
while loop |
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
A while loop statement in Swift programming language repeatedly executes a target statement as long as a given condition is true.
Syntax
The syntax of a while loop in Swift programming language is −
while condition { statement(s) }
Here statement(s) may be a single statement or a block of statements. Thecondition 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 ! ornot returns a special false value.
Flow Diagram
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.
Exampleimport Cocoa var index = 10 while index < 20 { println( "Value of index is \(index)") index = index + 1 }
Here we are using comparison operator < to compare the value of the variableindex 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 |
do...while loop |
Like a while statement, except that it tests the condition at the end of the loop body.
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least once.
Syntax
The syntax of a do...while loop in Swift is −
do { 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 do, 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 ! ornot returns a special false value.
Flow DiagramExampleimport Cocoa var index = 10 do { println( "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 |
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Swift supports the following control statements. Click the following links to check their detail.
Control Statement | Description |
---|---|
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.
The continue statement in Swift tells a loop to stop what it is doing and start again at the beginning of the next iteration through the loop.
For a for loop, the continue statement causes the conditional test and increments the portions of the loop to execute. For while and do...while loops, the continue statement causes the program control to pass to the conditional tests.
Syntax
The syntax for a continue statement in Swift is as follows −
continue Flow DiagramExampleimport Cocoa var index = 10 do{ index = index + 1 if( index == 15 ){ continue } println( "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 |
break statement |
Terminates the loop statement and transfers execution to the statement immediately following the loop.
The break statement in C programming language has the following two usages −
If you are using nested loops (i.e., one loop inside another loop), then thebreak statement will stop the execution of the innermost loop and start executing the next line of the code after the block.
Syntax
The syntax for a break statement in Swift is as follows −
break Flow DiagramExampleimport Cocoa var index = 10 do{ index = index + 1 if( index == 15 ){ break } println( "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 |
fallthrough statement |
The fallthrough statement simulates the behavior of swift switch to C-style switch.
A switch statement in Swift completes its execution as soon as the first matching case is completed instead of falling through the bottom of subsequent cases as it happens in C and C++ programming languages.
The generic syntax of a switch statement in C and C++ is as follows −
switch(expression){ case constant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); }
Here we need to use a break statement to come out of a case statement, otherwise the execution control will fall through the subsequent casestatements available below the matching case statement.
Syntax
The generic syntax of a switch statement in Swift 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 theswitch 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 programming without fallthrough −
import Cocoa var index = 10 switch index { case 100 : println( "Value of index is 100") case 10,15 : println( "Value of index is either 10 or 15") case 5 : println( "Value of index is 5") default : println( "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 programming with fallthrough −
import Cocoa var index = 10 switch index { case 100 : println( "Value of index is 100") fallthrough case 10,15 : println( "Value of index is either 10 or 15") fallthrough case 5 : println( "Value of index is 5") default : println( "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 |