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

About Author

Related Posts

C# Reference Types

Understanding C# Reference Types

One of the key features of C# is its support for C# reference types, which allow developers to create complex, object-oriented applications. In this blog post, we…

c# value types

Understanding C# Value Types

C# is a powerful programming language that is widely used for developing a wide range of applications. One of the key features of C# is its support…

C# check if server is online

C# check if server is online directly from your code. Check servers or services like web servers, database servers like MySQL and MongoDB. You can probably check…

C# Convert Int to Char

C# convert int to char google search is giving some result which I think is not directly what some people want. Most results are giving instructions on…

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…

Leave a Reply