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

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 with...
Comments in C Comments in C Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types o...
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...
Switch Case Statement Switch Case Statement The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of c...
Introduction To C A Brief History Of C   C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the r...
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