Control Flow In Swift

Introduction

 
Swift includes for and while loops to perform a task multiple times, and if and switch statements to execute different branches of code based on conditions. Statements like break and continue to transfer the flow of execution to another point. Swift has a for-in loop which is used to iterate over arrays, dictionaries, range, string, and other sequences. The case of switch does not fall through the next case in Swift by using the break statement. Cases match many different types of patterns, including range matches, tuples, and pertain to a specific type. 
 

For Loop

 
A for loop performs a set of statement a certain number of times Swift provides two types of loops:
 
For-in
 
performs a set of statements for each item in a range, sequence, collection, or progression.
 
For-condition-increment
 
It performs a set of statements until a specific condition is met, by incrementing a counter each time loop ends.
 

For-In

 
The for-in loop is used to iterate over collections of items, Such as ranges of numbers, items in array or characters in string.
  1. let base = 3    
  2. let power = 10    
  3. var answer = 1    
  4. for_in 1..power    
  5. {    
  6.  answer *= base    
  7. }    
  8. println("\(base) to the power of \(power) is \(nswer)")    
Output
 
3 to the power of 10 is 59049
 

For-condition-increment

 
Swift has a condition for the for loop with an incrementer. Semicolons separate the three parts of loop and Swift does not need parenthesis around the entire initialization condition, increment block. The initialization expression is evaluated once to set up any constant or variables for the loop and condition expression is evaluated. The code execution continues by executing the statements inside the braces. The format of the loop and execution process is described in the following:
  1. initialization    
  2. while condition    
  3. {    
  4.  statements    
  5.  increment    
  6. }    
Constants and variables are declared within the initialization expression and they are valid within the scope of for loop. To retrieve the final value of index, after the loop ends, it must be declared as index before the loop scope.
  1. var index : Int    
  2. for index = 0;index < 3; ++index    
  3. {    
  4.  println("Index is \(index)")    
  5. }    
  6. println(" \(index) times the loop is executed ")    
Output
 
Index is 0 
Index is 1
Index is 2
 
The loop is executed 3 times.
 

While loop

 
A while loop performs a set of statements until the condition is false else it executes infinite times. This loop is used when the number of iteration is not known before the first iteration begins. It has two types of while loop. While evaluates its condition at the start of each pass through the loop. Do while evaluates its condition at the end of each pass through the loop.
 
A while loop start by evaluating a single condition. A set of statement is repeated until the condition becomes false.
  1. while condition      
  2. {      
  3.  statements      
  4. }      
  5. //syntax  
  6. var square = 0    
  7. var diceroll = 0    
  8. while square < finalSquare    
  9. {    
  10.  if ++diceroll == 7    
  11. {    
  12.  diceRoll = 1    
  13. }    
  14.  square += diceRoll    
  15. if square < board.count    
  16. {    
  17.  square += board[square]    
  18. ("Over")     

Do while

 
A do while loop perform a single pass through the loop block first before considering loop condition. It continues to repeat the loop until the condition is false. Do while executes the loop once and checks for the condition, then moves to the number of times the loop required to be executed. Then it execute the loop until the condition is met.
  1. do    
  2. {    
  3.  statements    
  4. }    
  5. while condition    
  6. //syntax  
  7. do  
  8. {  
  9.  square += board[square]  
  10.  if ++diceroll == 7  
  11. {  
  12.   diceRoll = 1  
  13. }  
  14. square += diceRoll  
  15. }  
  16. while square  < finalSquare  
  17. println("Over")  

Switch

 
A switch statement considers a value and compares to possible matching patterns and executes an appropriate block of code. It is similar to the if statement for multiple potential states. Switch statement compares the value against one or more values of the same type. Switch statement consist of multiple possible cases and has a case keyword. Every switch must be exhaustive and every possible value of type is considered by switch cases. 
  1. switch 2  
  2. {  
  3.  case 1:  
  4.   println("1")  
  5.  case 2:  
  6.   println("2")  
  7.  default  
  8.   println("Not found")  
  9. }