iOS Development with Swift 2 - Loops



Swift provides a variety of Control Flow Statements. Loops are generally used to iterate over a condition or statement multiple times, until a Condition / Programmer’s need from that loop is satisfied. Swift provides the following kind of loops.

For-in loop

To perform a task multiple times. We use the ‘for-in loop’ to iterate over a sequence, such as an array, range of numbers or characters in the string.

Let us consider the following example

var items = [1,2,3]  
for item in items {   
   print(item) 
} 

The output would be as follows −

1 
2 
3

While loop

The While Loop is used to perform a task multiple times. It is used when we need to iterate over a condition until it becomes false. The While loop is used best when the number of iterations is not known at start. While loop is of the following two types −

  • While Loop
  • Repeat-While

A while loop starts by evaluating a single condition, if the condition is true, then it iterates over the loop until the condition becomes false. The syntax of the while loop is as follows −

while (condition) { 
   Statements; } 

Let us consider the following example.

var i = 2 
while (i > 0) { 
   print(i) 
   var i = 2 
   while (i > 0) { 
      print(i) 
      i = i - 1; 
   } 
} 

Its output would be as follows −

2 
1

Repeat-while loop

This is another version of while loop. In this loop, the control passes through the statement at least once before checking the condition. The syntax of the repeat-while loop is as follows −

repeat  { 
   statements 
} 
while(condition)

Let us consider the following example.

repeat { 
   print(i) 
   i = i - 1;  
} 
while (i>0)

The output would be as follows −

2 
1 

Conditional Statements

These type of statements are often useful when we have to execute some code depending on some condition. You might want to perform different actions on different input from the user.

In such cases, conditional statements are very helpful. Following are the conditional statements available in swift.

‘If’ Conditional Statement

If condition is the smallest possible condition, which executes a set of lines only if the condition is satisfied.

The syntax of the if condition is as follows −

if (condition) { 
   Statements to execute } 

Let us consider the following example −

var x = 5 
if (x > 4) { 
   print(x)  
}

Its output would be as follows −

5

‘Else’ Conditional Statement

This condition is used with the ‘IF’ condition, if the ‘IF’ condition fails, then the control will come to else. The syntax of the else condition is as follows −

if(condition) {   
   Statement to execute  
} 
else  {  
   Statement to execute 
} 

Let us consider the following example.

var x = 6 
if(x > 6) {  
   print("this is if condition") 
} else {  
   print("this is else condition") 
}

Its output would be as follows −

this is else condition

If–else Conditional Statement

The "if else" conditional statement is used when we have more than one condition, which we want to check. For example, first we want to check if the value is smaller than 4, we will print loop 1, else if the value is between 4 and 8 we will print loop 2, and else if the value is greater than 8 we will print loop 3.

The syntax of the if–else condition is as follows −

if(condition) { 
   Statement to execute } 
else if() {  
   Statement to execute } 
else {  
   Statement to execute } 

Let us consider the following example.

var x = 6 
if(x <4) {   
   print("loop 1") 
} else if(x>4 && x<8) { 
   print("loop 2") 
} else {  
   print("loop 3") 
}

Its output would be as follows −

loop 2

Switch Conditional Statement

The Switch statement considers a value and compares it against several possible matching patterns. A switch statement provides an alternative to the ‘IF’ statement by responding to multiple states.

Switch cases are Case sensitive, i.e. ‘a’ & ‘A’ are different.

The syntax of the Switch statement is as follows −

switch value to consider {  
   case value1 : response for value one. 
   case value2, value3 : response for value 2, value3 
   default : if none of the value matches, do this. 
} 

Let us consider the following example

let somechar: character = 'c' 
switch somechar {  
   case "a" : print("first Alphabet") 
   case "c" : print("Third Alphabet") 
   default : print("anything other") 
}

Its output would be as follows −

Third Alphabet

Control Transfer Statements

These statements change the order in which your code is executed, by transferring your control from piece of code to another.

Following are the type of control transfer statements available in swift.

  • Continue − This statement tells the loop to stop whatever it is doing and continue the loop from the beginning of the next iteration.

  • Break − The break statement stops the execution of loop and exits the loop. It transfers the control to the code written after that loop.

  • Return − This statement returns some value to its parent.

Functions

Functions are a block of code that execute some code and return the result. Functions have a name using which they are called, they have a return type and they produce a result of that type only. Swift functions may have any parameter or not, but they are flexible.

Defining a function

When we define a function, the function must have a name, a return type and optionally some input that the function would take, which are known as parameters.

The syntax of defining a function is as follows −

func functionName(parameterName : parameterType) -> returnType { 
   // code 
   return (some Value of returnType) 
} 

Let us consider the following example.

func sayHello(personName : String) -> String { 
   let greeting = "Hello" + personName + "!" 
   return greeting 
} 

Calling a Function

After writing a function, the call must be made using the function name, and if it has some parameters, then they must be passed in the function calls.

If we want to call the above function, we should use the following command −

sayHello(TutorialPoint)

Its output would be as follows −

Hello TutorialPoint!

A function can be without parameters, it can have multiple parameters or it can be without a return type also.

ios_development_with_swift2_playground.htm
Advertisements