Introduction to Swift Control Flow

Swift provides us with all the control flow that are available in c language and objective C. As the term control flow is self explanatory, let me give more clarifications on the control flow. These are the constructs that allows us to control the behavior of our program execution. For example For Loop helps us in iterating through a bunch of collection. Switch statement helps us to execute a particular thing based on user selection. There are so many control flow constructs available in Swift and they are listed below

For Loops

 In Swift you can use For Loop as

Simple For Loop :

for var i = 0; i<6 i=”” p=””>{
   println(“The value of i is (i)”)
}

Above code will print

The value of i is 0
The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5
The value of i is 6

In generic term syntax for For Loop

for initialization;condition;increment
{
    code to be executed
}


For in :

You can use For in Loop to iterate over a collection of items such as arrays,dictionaries etc.

let myArray = [“object 1”, “object 2”, “object 3”]

for str in myArray
{
    println(“myArray contains (str) ” )
}

Output will be

myArray contains object 1
myArray contains object 2
myArray contains object 3

While Loop :

While loop execute a set of instructions until condition became false

while(condition)
{
    code to be executed
}

var i = 1;

while(i<5 p=””>{
    println(“The value of i = (i)”)
    i++;
}

Output will be

The value of i = 1
The value of i = 2
The value of i = 3
The value of i = 4

Do while:

Do while will execute code first and then check for the conditional check

do
{

  code to be executed

}while(condition) 

Conditional Statement:

These are the control flow constructs that execute a chunk of code based upon the success of condition

if statement

This is the basic conditional statement

if condition
{
   code to be executed
}
else
{
    code to be executed
}

let turn =1

if turn ==1
{
   println(“Inside If block”)
}
else
{
   println(“Inside else block”)
}

else if statement

if condition
{
   code to be executed
}
else if condition
{
    code to be executed
}
else
{
    code to be executed
}

let i = 2

if i ==1
{
   println(“Inside If block”)
}
 else if i ==1
{
   println(“Inside else block”)
}
else
{
   println(“Inside else block”)
}

Switch statement

Switch  statement take a value and match it against  several cases

Switch value to consider
{

 case value 1: 
                     code to execute for value 1
 case value 2: 
                     code to execute for value 2
 case value 3: 
                     code to execute for value3
case default:
                     code to execute for default case

Switch in swift is different for objective C and C as in C and objective C switch statement falls to next case after executing a case and thus require break keyword, whereas in Swift control comes out of Switch as soon as its execute the case. Thus making switch safer to user as compared to  C and Objective C.