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

Swift Basics - Loops.

That was the question:

Given an array of numbers, print out each number in the array using a while loop and the println statement.

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Ok, so I did what they've asked to print.

var index = 0
while index < numbers.count {
println(numbers[index])
index++ }

But I don't really get it. What the "printIn(numbers[index])" means (it's printing the numbers and the index?) and neither why I have to use the "index++" at the end (the index++ is adding 1 to something?). Would really appreciate a detailed explanation guys! I can't really get it :( .

1 Answer

I'm not particularly familiar with swift, but I can give you a general description of arrays.

As you may now, an array is a collection of values (or items, or whatever you want to call it):

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

What this means is that you declare numbers to an array of integers. You can access the individual numbers using the index, which starts at 0. So numbers[0] gets you the value it the first position, which is 1, numbers[1] gets you the value in the second position and so on.

In light of this, let's have a look at your code:

var index = 0                   //Create an index variable (exactly what it 
                                //sounds like, given the previous explanations
while index < numbers.count {   //This performs the operations in the curly
                                //braces while the condition is true. In this
                                //case, while index is less than the number of 
                                //items in the array numbers

  println(numbers[index])       //Here, you print the value numbers hold AT
                                //the current index, but not the index itself

  index++                       //Incrementing the index makes it so that the
                                //next time the loop performs the operation,
                                //it does so for the next index position, i.e.
                                //index is 0, 1, 2, ..., 9, and the values
                                //printed are 1, 2, 3, ..., 10.
}

That's the basics. I hope that this was helpful, and if you have any questions don't hesitate to ask ^^

Edit: Having read about control flows in Swift, I noticed that there is a for-in loop, which allows to write:

for item in numbers {
  println(item)
}

To accomplish exactly the same thing as the while loop. You could also use an ordinary for loop like so:

for var index = 0;  index < numbers.count; index++{
  println(numbers[index])
}

Ok, so what I understood from your explanation is that: we create an index variable and then we say that if the variable index is less than our numbers.count (10) the condition is true and he can perform what we want him to do. What we want him to do is what we're saying next: (that we want to print the numbers at the current variable?!). I understood the index part, but if we don't include him in the code we start an infinite loop. And he starts again when it arrives at 10? Like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, (..) and it goes to infinite and that's why we put the index++? So we can control it because after the 4th index we do not have a 6th number to compose in the loop? And either way why he stops the loop? Why it doesn't come back to one and starts over? Your answer really helped! But I still have doubts..

Not quite. I think that you got the part about accessing the values and that unless we increment the index variable the while loop will go forever, but it will access the same index every time it loops as well:

//This, as I think you'll notice, is exactly the case you mentioned.
//Since index never increments we will never get out of the loop,
//and will actually just get lines with a 1 printing forever.

var index = 0
while index < numbers.count {   //Far any array containing 1 or more elements
                                //the condition will always be true

    println(numbers[index])     //This is where you have a slight misconception.
                                //println will print the number at index,
                                //and since index never changes, it will always
                                //print the same thing.
                                //Accessing the value in this way doesn't
                                //actually do anything the array or its content,
                                //so if it's [1,2,3,4,5] on the first pass,
                                //it will be [1,2,3,4,5] on the next pass as
                                //well.
}

I don't quite understand with your next question, and if you feel that it's still unclear, please, do ask!!

Oh you saved me! Haha. Thanks very much, really appreciate! Now I can continue the course without a problem with loops. And as you mentioned the "for loop" is really simpler, I think I'll use it from now on.

Happy to help ^^ The for loop is indeed the better option in this case, but I really recommend that you try to get comfortable with all kinds of loops since they have different uses. Happy coding!