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 {
    Statement
}
  • 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
/* 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)!")
 }
 //Output:- 
 // 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 {
    statements
}

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
}
print(sum)
//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 {
    statements
} while condition
number = 10
repeat {
   print(number)
   number = number - 1
} while number > 0
/* Output: -
10
9
8
7
6
5
4
3
2
1 */

Get more stuff like this

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.