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

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 fu...
Variables Variables A variable is a named location in memory that is used to hold a value that can be modified by the program. All variable must be declared be...
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. ...
Decision Control Statement C has three major decision making instructions— the if statement, the if-else statement, and the switch statement. The if Statement It is used to ex...
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 xib in swift 3.1 and xcode 8.3.2 . Custom views are common and...
How to use Flutter most usable Widgets | Flutter t... We need some basic widgets for creating our first flutter app. Widgets describe what their view should look like given their current configuration and...

Leave a Reply

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

Get more stuff like this
in your inbox

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