Control Flow : Swift tutorial

Swift Provides diffrent type of controll flow statements like:

  • Loops Statements : Loops are used for perform the task multiple times or for handel the arays, dictionaries, string etc
  • Conditional Statements : Execute some line of code at certain condition.
  • Control Transfer Statements : Change the order in which your code is executed, by transferring control from one piece of code to another.

Loops Statements

  • Loops are used for execute Number of line of code multiple times/statement is execute sequentially. This execution is depending on condtions is true/false.
  • Swift have these loopes statement
    • For-In loops
    • While loop
    • Repeat-While loop
1. For-In loop

For-In loop is used when we need to execute set of code at certain number of time in repeated manner.
For-In loop in swift syntax are following :

for item in Range {
  • Uses a for-in loop to iterate over the items in an array :
let names = ["A", "B", "C", "D"]
for name in names { 
print("Show, \(name)!")
/* outpout:-
 Show, A!
 Show, B!
 Show, C!
 Show, D! */        
  • Uses Half upper Range operator (..<) to include lowwer bound but not upper bound. By this operator we can run for loop at perticular range. We can follow this with other range operators also like “One-Sided Ranges(...14...)”, “Closed Range Operator(1...5)”
 for item in 2..<names.count {
 print("Show, \(item)!")
/* Output :-
 Show, 2!
 Show, 3!
 Show, 4! */
  • If you don’t need each value from a sequence, you can ignore the values by using an underscore in place of a variable name.
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
    answer *= base
print("\(base) to the power of \(power) is \(answer)")
// Prints "3 to the power of 10 is 59049"
  • use with stride(from:to:by:), Its use for a constant gap between in sequence. And it return the sequence starting value but not end value.
 swiftlet minutes = 60
 let minuteInterval = 5
 for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
 print("Show, \(tickMark)!")
 // render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
  • We can also use For-In loop with dictionary. Each item of dictionary is getting in (key, value)pair.
 let numberOfLegs = ["dog": 8, "ant": 6, "cat": 4]
 for (animalName, legCount) in numberOfLegs {
 print("\(animalName)s have \(legCount) legs")
 /* Output:-
ants have 6 legs
cats have 4 legs
dogs have 8 legs */
2. While loop

while loop performs a set of statements until a condition becomes false. While loop is used when we not know the number of Running of set of statement before first execution begin.
While loop in swift syntax are following :

while condition {

while loop starts by evaluating a single condition. If the condition is true, a set of statements is repeated until the condition becomes false.

var number = 10
var sum = 0
while number != 0 {
    sum = sum + number
    number = number - 1
//Output: - 55
3. repeat-while loop

The other variation of the while loop, known as the repeat-while loop, performs a single pass through the loop block first, before considering the loop’s condition. It then continues to repeat the loop until the condition is false.
repeat-while loop in swift syntax are following :

repeat {
} while condition
number = 10
repeat {
   number = number - 1
} while number > 0
/* Output: -
1 */

Related Post

Firebase Push notification | Swift tutorial Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account. You need to Enroll into the Apple D...
Continue Statement in JAVA Continue Statement in JAVA Suppose you are working with loops. Sometime it is desirable terminate the loop or skip some statement inside the loop wit...
Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...
Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use of xib in swift 3.1 and XCode 8.3.2 . Custom views are common ...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Create Gradient layer by the use of CAGradientLaye... Create Gradient layer by the use of CAGradientLayer Mostly developers are use the gradient (Combination of colors) colors images for make app more ef...

Write A Comment