Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Subject Swift

can somebody please explain me the difference between the while loop for in loop and repeat while loop and also explain me what this things are doing with easy words. Could you maybe give me an example too?? thanks

1 Answer

Hey!

A while loop does the code in the while loop whilst something is true.

For example:

If we had the code:

var count = 10

while count > 0 {
    print("Hello World, Count = \(count)")
    count -= 1
}

Then then the results would be:

Hello World, Count = 10
Hello World, Count = 9
Hello World, Count = 8
Hello World, Count = 7
Hello World, Count = 6
Hello World, Count = 5
Hello World, Count = 4
Hello World, Count = 3
Hello World, Count = 2
Hello World, Count = 1

This happens because whilst the variable 10 is greater than 0 then the code in the while loop will run.

However a For In loop does code for each thing you pass in it.

For Example

If we had this code:

var array = ["One","Two","Three","Four"]

for arrayElement in array {
    print("This Array Element Is \(arrayElement)")
}

Then it would take each array element and parse it into my print statement. Basically, what this is doing is splitting each element in my array up and calling it "arrayElement" and then for each element I am running the statement:

print("This Array Element Is \(arrayElement)")

So, results I will get are:

This Array Element Is One
This Array Element Is Two
This Array Element Is Three
This Array Element Is Four

Another use for a "For In" loop is if we did something like this:

for number in 1...5 {
    print("Index Number is \(number)")
}

What we are doing here is instead of using an array we are using a range from 1 to 5 so instead of an array the numbers 1, 2, 3, 4, 5 are going to parsed into the statement instead.

The results we will get this time are:

Index Number is 1
Index Number is 2
Index Number is 3
Index Number is 4
Index Number is 5

Lastly, For a Repeat while loops we could do something like this:

repeat {
    print("Hello World, Count is = \(count)")
    count -= 1
} while count > 0

This is the same as out while loop but we are putting our statements in a repeat thing. Then we are specifying our while loop condition. This is the same as the while loop but it runs the statement first then it looks at the while loop condition . I hope this helps you! If you have any questions please feel free to ask me! and if any of this doesn't make sense please also feel free to ask me to explain it!

Goodluck with learning swift!