Swift loops – Basic looping in swift

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 {

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++ {

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 {

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 {

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 {
if index > 9 {

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 {
} 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 {
} 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!

Related Posts

c# bash script

C# Bash Script Made Easy

There are many reasons why it could be handy to run a bash script from a C# application. In 2014, before I changed to a Mac as…

go hello world

Golang Hello World, Get a Easy Fantastic Start

Golang hello world example tutorial. I assume you are new to the golang language since you found this website. Go is one of the latest programming languages…

Unity persistant datapath

Unity Persistent Data Path

The Unity persistentDataPath is read only and will return the path where you can save data that will be persitent betwen sessions. Even app updates doesn’t touch…

csharp ping check

C# Ping Check the easy way

How can we check if a computer, server, or another device is online? We can use a C# Ping check. If you can check the device with…

C# return index of a object

C# return index of a object in a List

How to return the index of a list type that is an object in C#. Most examples only shows simple string types when they show how to…

c# 99 bottles of beer

C# 99 bottles of beer on the wall

C# 99 bottles of beer on the wall is an easy programming challenge. The challenge can be solved in at least dozens of ways. Here is one…

Leave a Reply