Control Flow Statements | Swift tutorial

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
    • 1
      For-In

       loops

    • 1
      While

       loop

    • 1
      Repeat-While

       loop

1. For-In loop
1
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 :


1
2
3
<span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> <span class="hljs-type">Range</span> {
    <span class="hljs-type">Statement</span>
}
  • Uses a 
    1
    for-in

     loop to iterate over the items in an array :


1
2
3
4
5
6
7
8
9
<span class="hljs-keyword">let</span> names = [<span class="hljs-string">"A"</span>, <span class="hljs-string">"B"</span>, <span class="hljs-string">"C"</span>, <span class="hljs-string">"D"</span>]
<span class="hljs-keyword">for</span> name <span class="hljs-keyword">in</span> names {
<span class="hljs-built_in">print</span>(<span class="hljs-string">"Show, <span class="hljs-subst">\(name)</span>!"</span>)
}
<span class="hljs-comment">/* outpout:-
 Show, A!
 Show, B!
 Show, C!
 Show, D! */</span>
  • Uses 
    1
    Half upper Range operator (..&lt;)

     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(

    1
    ...1

    1
    4...

    )”, “Closed Range Operator(

    1
    1...5

    )”


1
2
3
4
5
6
7
8
 <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> <span class="hljs-number">2</span>..&lt;names.<span class="hljs-built_in">count</span> {
 <span class="hljs-built_in">print</span>(<span class="hljs-string">"Show, <span class="hljs-subst">\(item)</span>!"</span>)
 }
 <span class="hljs-comment">//Output</span>
<span class="hljs-comment">/* Output :-
 Show, 2!
 Show, 3!
 Show, 4! */</span>
  • 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.

1
2
3
4
5
6
7
8
<span class="hljs-keyword">let</span> base = <span class="hljs-number">3</span>
<span class="hljs-keyword">let</span> power = <span class="hljs-number">10</span>
<span class="hljs-keyword">var</span> answer = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> <span class="hljs-number">_</span> <span class="hljs-keyword">in</span> <span class="hljs-number">1</span>...power {
    answer *= base
}
<span class="hljs-built_in">print</span>(<span class="hljs-string">"<span class="hljs-subst">\(base)</span> to the power of <span class="hljs-subst">\(power)</span> is <span class="hljs-subst">\(answer)</span>"</span>)
<span class="hljs-comment">// Prints "3 to the power of 10 is 59049"</span>
  • use with 
    1
    stride(from:to:by:)

    , Its use for a constant gap between in sequence. And it return the sequence starting value but not end value.


1
2
3
4
5
6
7
 swiftlet minutes = <span class="hljs-number">60</span>
 <span class="hljs-keyword">let</span> minuteInterval = <span class="hljs-number">5</span>
 <span class="hljs-keyword">for</span> tickMark <span class="hljs-keyword">in</span> <span class="hljs-built_in">stride</span>(from: <span class="hljs-number">0</span>, to: minutes, by: minuteInterval) {
 <span class="hljs-built_in">print</span>(<span class="hljs-string">"Show, <span class="hljs-subst">\(tickMark)</span>!"</span>)
 }
 <span class="hljs-comment">//Output:- </span>
 <span class="hljs-comment">// render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)</span>
  • We can also use 
    1
    For-In

     loop with dictionary. Each item of dictionary is getting in 

    1
    (key, value)

    pair.


1
2
3
4
5
6
7
8
 <span class="hljs-keyword">let</span> numberOfLegs = [<span class="hljs-string">"dog"</span>: <span class="hljs-number">8</span>, <span class="hljs-string">"ant"</span>: <span class="hljs-number">6</span>, <span class="hljs-string">"cat"</span>: <span class="hljs-number">4</span>]
 <span class="hljs-keyword">for</span> (animalName, legCount) <span class="hljs-keyword">in</span> numberOfLegs {
 <span class="hljs-built_in">print</span>(<span class="hljs-string">"<span class="hljs-subst">\(animalName)</span>s have <span class="hljs-subst">\(legCount)</span> legs"</span>)
 }
 <span class="hljs-comment">/* Output:-
ants have 6 legs
cats have 4 legs
dogs have 8 legs */</span>
2. While loop

1
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 :


1
2
3
<span class="hljs-keyword">while</span> condition {
    statements
}

1
while loop

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


1
2
3
4
5
6
7
8
<span class="hljs-keyword">var</span> number = <span class="hljs-number">10</span>
<span class="hljs-keyword">var</span> sum = <span class="hljs-number">0</span>
<span class="hljs-keyword">while</span> number != <span class="hljs-number">0</span> {
    sum = sum + number
    number = number - <span class="hljs-number">1</span>
}
<span class="hljs-built_in">print</span>(sum)
<span class="hljs-comment">//Output: - 55</span>
3. repeat-while loop

The other variation of the 

1
while

 loop, known as the 

1
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 

1
false

.

1
repeat-while

 loop in swift syntax are following :


1
2
3
<span class="hljs-keyword">repeat</span> {
    statements
} <span class="hljs-keyword">while</span> condition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
number = <span class="hljs-number">10</span>
<span class="hljs-keyword">repeat</span> {
   <span class="hljs-built_in">print</span>(number)
   number = number - <span class="hljs-number">1</span>
} <span class="hljs-keyword">while</span> number &gt; <span class="hljs-number">0</span>
<span class="hljs-comment">/* Output: -
10
9
8
7
6
5
4
3
2
1 */</span>

Related Post

UIPopOverPresentationController In Swift UIPopOverPresentationController UIPopOverViewController in swift is writen in swift 3.1 and Use the xcode Version 8.3.2. For use this import the Pop...
Introduction to JVM Introduction to JVM JVM stands for Java Virtual Machine. It works as an engine that derives the Java Code. The JVM does not understand Java ty...
Dart Introduction | Dart programming language Dart Introduction Dart is a scalable language that you can use to write simple scripts or full featured apps. Whether you’re creating a mobile app, w...
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...
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...

Leave a Reply

Your email address will not be published. Required fields are marked *