Muhammad Harits Syaifulloh

Friday, October 21, 2016

Swift 3 - Loops In Swift 3

Swift 3 - Loop In Swift 3 Ask MHS


Occasionally in your programs you need to perform some kind of action multiple times over multiple pieces of data. And rather than calling some function again and again and again. Loops allow you to do than in just a few lines of code. So let's see how this loops work and all the different kinds that we have in Swift 3 by implementing them in our playground.

The very first and most basically loop is available in most languages and it is called the For loop. So let's write one, start from the following code:


Now what the above loop is doing it's creating a variable called i and it's assigning the numbers zero, one, two, etc., to i. That's what the above loop means. Every time the loop goes round i = 0, i = 1, i = 2 all the way until i is less than 10.  So i is going to reach the number 9, it's going to go from 0 to 9, but never get to the number 10because that's the condition we've specified. And we've said each time you go around I would like you to print i out, and that's why we see over on the right "10 times". So the result will look like the following picture:


So that's the basic for loop. Now if you programmed Swift 3 before, you might have done it a slightly different way that's recognized languages the world over where we would have an old style of for loop in Swift. But now in Swift 3 it will not work, so if you come across web tutorials an they recommended the old for statement, just skip it and let's delete it from our memories right now.

Alright, so when you would use a for statement. Well, if you had an array of stuff and you wanted to go through each item and print it off or do some addition to it or whatever you wanted that's where for loop really come into their own. Let's have the following code :


So the above for loop is going to go through each number one by one in that array starting from index 0 and going through it element by element. So that's you grab each element from an array.

Now we have the other different kind of loop and this is called by while loop. So let's ave the following code :


According to the above while loop, count is declared with 0 value. Then while loop will check if count is less than 10, it will execute print(count). Then count += 1 means the current count value will added by 1, so it will have value 0, 1, 2, until count had met the condition that's less than 10. And if count is equal or more that 10, print(count) will not be executed.

Now this is the final version of loop in Swift 3, and this is called repeat while loop. Where we ask to repeat a certain function, so let's have the following example of code:


And there we have it, so that's repeat while loop. And the different between while loop and repeat while loop is the repeat performs the function first. But while loop performs it as part of the loop itself.

Alright guys, that's all about loop in Swift 3. Hope it will be understood easily. If you have any question just leave some comment below and I'll reply it soon. Thank you very much for visit my blog and see you next time in the next tutorial.

No comments:

Post a Comment