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

Navigation Tab Bar in IOS | Swift tutorial Navigation Tab Bar in IOS | Swift tutorial Different Customizing techniques for Navigation bar with storyboard and code 1) Custom NavigationTabBar by ...
Getting started with C Getting started with C : If we want to communicate with our computer it is very much needed that our computer understands what we want to say to it. ...
New features in XCode 10 Supported Configurations Xcode 10 beta requires a Mac running macOS 10.13.4 or later Xcode 10 beta includes SDKs for iOS 12, watchOS 5, macOS ...
New Tools for 64-bit Support in Xcode 9.3 New Tools for 64-bit Support in Xcode 9.3 The last macOS release to support 32-bit apps without compromise is macOS High Sierra. Make sure future rele...
Data Type And Storage And Keywords To store data inside the computer we need to first identify the type of data elements we need in our program. There are several different types of dat...
Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...

Write A Comment