Swift loops – Basic looping in swift


Swift loops

Swift loops

Some basic introduction to Swift loops.

Swift Loops: For loop

For loops are the same as the traditional C loops. If you have experience from C/C++ you already know this well. This is structured like this.

for initialisation; condition; increment {
statement
}

An example on how this looks in real life. We here initialise an variable called counter. The variable can of course be named what ever you want.

for var counter = 0; counter < 10; counter++ {
print(counter)
}

This little example will print out the numbers from 0 to 9. When setting the condition to < 10, it means the for loop will run as long as counter is below 10. As soon as the counter reach the number 10, it will stop.

Swift Loops: For-In

As far as I know this is new with Swift. But if you know C/C++ (and probably others) it is very similar to For Each loops.

For-In loops runs your statements for each item in a sequence. That sequence can be from a number to another number. Like 0 to 10 as in the example above. Or it can be for each item in an collection, like an array. Lets look at two different ways of using For-In loops.

First example show how to use it like a regular for loop.

for counter in 0...9 {
print(counter)
}

You might notice we didn’t write 0…10. That is because 0…9 is not a condition, it is items we want to run through. So 0…9 in For-In loops equals Counter=0; Counter<10; Counter++ with regular For loops.

We can also run through arrays with For-In loops. Here is an array example.

let myCars = ["Ferrari", "Porsche", "Lada", "Skoda"]
for car in myCars {
print(car)
}

This will print out all the cars in the myCars array.

Swift Loops: While

Same as with for loops, while loops works like traditional C/C++ while loops. Like for loops while loops have an condition. But there no increment part in while loops. You need to take care of that your self, if thats how you want to run the while loop. We will look at that.

To use while about the same way as for loops.

var counter = 0
while counter < 10 { print(counter) counter++ } This will print out the numbers 0 through 9. And as you see the counter increments with the other statements inside the while loop. You can also set the while condition to true. And use a break statement to end the while loop. Here is an example on how to do that.

index = 0
while true {
print(index)
index++
if index > 9 {
break
}
}

Swift Loops: Repeat While

Repeat while are similar to do while from traditional C. And it works the same way. The format of a repeat while loop is:

repeat {
statement
} while condition

So like the last while example, we need to use statements to manipulate the conditions to end the loop. Here is a very simple example on a repeat while loop.

var repeats = 0
repeat {
repeats++
} while repeats < 10

Like all the others example on this page, this will print out the numbers from 0 to 9.

Here is Apples swift documentation about control flow

Thats it for this time.

Happy looping!

Have any Question or Comment?

Leave a Reply