iOS

iOS Lesson 09 – Practicing with Loops in Swift

So with this knowledge bit, we are going to extend the knowledge of Arrays and get a look at how Loop in Swift can help us solve complex queries in programming.

Open up your Xcode and create a new playground. We are going to call this one “LoopsForLife” and delete the Hello world line.

Loops in Swift:

In general, Loop is a pattern in which a certain instruction continues to run until a specific condition is reached.

In order to understand the advantage of a loop, you would need to understand the complexity of things that you will get without loops. To do that, let’s take a similar example of arrays that we have covered in the previous knowledge bit

Code:

var studentMarks : [Double] = [10.0,20.0,30.0,40.0,50.0]

Using the concept from previous knowledge bit, we have created a variable using the var keyword and named it as studentMarks. Then, we used the equal sign and then we gave it few values. We separated those values using the comma. This type of array will contain values of any type but if we declare its type explicitly then it will be forced to save a specific type.

  1. Q) What if we want to do some grading in marks of each student.
  2. A) With the knowledge about swift uptill now, you would probably do something like this.

Code:

studentMarks[0] = studentMarks[0]+(studentMarks[0] * 0.10)

studentMarks[1] = studentMarks[1]+(studentMarks[1] * 0.10)

studentMarks[2] = studentMarks[2]+(studentMarks[2] * 0.10)

studentMarks[3] = studentMarks[3]+(studentMarks[3] * 0.10)

studentMarks[4] = studentMarks[4]+(studentMarks[4] * 0.10)
       

We have written 5 lines of code just to increase the grade of each student by 10%. Let us say we have 100 students. Will you really type 100 lines of code? Don’t you think this will be a bad idea? If yes, then loops are the solution.

Repeat:

 Syntax for the repeat loop is as follows

Syntax:

var x = 0              //Line1

repeat{                 //Line2

x+=1                   //Line3

}while(x<array.count)  //Line4       
  • Line1, we have defined a variable that will be an index inside our array.
  • Line2, we have used “repeat” keyword to initialize our loop and with the { braces we have started the body of our loop.
  • Line3, we have increased our index by 1. If we don’t do that then our loop will run infinitely.
  • Line4, we are using the word while and after that we are giving it a condition “index is less than the number of elements inside array”

If you repeat it few times you will get the logic.

“While the condition is met, Repeat the code”

Example:

Loop in Swift practice

if you look closely it says on the right, 5 times, means that it ran this loop 5 times because there were 5 entries inside our studentMarks array

you are not going to use these repeat-while loops a lot, the main point of showing you this loop is to show you how easy it is to understand loops with its basic keyword syntax. It even makes sense when you look at it. What you are probably going to use more so is a for-in loop.

For-in

we are going to write similar thing with even fewer lines of code.

Syntax:

for i in 0..<array.count{

statement to loop through

}

in this scenario, we have used for a keyword to start our for loop then we place an identifier that will act as an index “i” then we used the keyword “in” to indicate the range in which our index is going to loop through. Then we have defined our range. In our case, we have defined from (0..<array.count). it means our loop is going to start from zero then we added 2 dots then the condition “less than array.count”

it means to start from zero and go all the way until the index become array.count-1.

If you want to define the exact range then you can use three dots to define the exact range.

Example:

Swift lesson: Loop in Swift

In this example, we have used both ways to define the range. Two dots with a condition or three dots to define the exact range.

For-Each:

Foreach loop will iterate through every element of the collection that has the condition of a loop.

Syntax:

for <identifier> in <collection>{

}       

in this syntax what we are doing is that we have just given an identifier and a collection and everything will be done under the hood. It will iterate through every entry of the collection and perform the actions that are mentioned inside the body of foreach loop.

foreach Loop in Swift

In short: for-each item in this array, run a loop. In our case, every mark inside the studentMarks array, print the marks. It doesn’t even care about the index because it will iterate through all the array elements one at a time. You don’t even have to worry about an infinite loop in this case.

P.S: if it seems confusing to you then it is alright. Its normal to be confused because you are definitely trying to learn how these loops work. Practice with these concepts a bit more and you will be good to go.

So with this knowledge bit, we have covered loops. We created one while loop and 2 for-in loops. we have seen how we can use them efficiently in the code to iterate in collections with ease. You can get all the code from here.

Please Like, share and subscribe to more!

 

You can have a look at our whole IOS series as well.

Click to comment

Most Popular

To Top