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 trialMarquett Burton
511 Pointswhile index < numbers.count { --Why does the first line print as 0 is not less than 0?
It seems that this piece of code relies on the idea that 0 is less than 0 when they are equivalent. Correct?
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
John Magee
Courses Plus Student 9,058 Pointsnumbers.count is not 0 - numbers.count is a count of the number of elements in the aray, 10? I don't know why you think 0 is comparing to 0
kjvswift93
13,515 Pointsvar index = 0 is simply setting 0 as the initial value for this variable named index, which will be used within the println statements. With the while loop in place followed by "index++", index will no longer be 0 when it sees that there is an element inside of the array. Since there is an element inside of the array (the number 1) that means the println function must be called because one is greater than 0. Index is now = 1, so it prints "1". Then, since it sees another element after the first one, index must then = 2 so as to print out "2". Index never stops doing this until there are no more elements left in the array.
1 Answer
kjvswift93
13,515 PointsI think that I can help explain it so that you will understand what is going on here a little better. First, I'll show you what the correct answer looks like below:
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 0
while index < numbers.count {
println (numbers[index])
index++
}
I used a variable named index and assigned it to the Int value 0. Essentially the next line beginning with the word 'while' states "as long as the number of elements in the numbers array are greater than 0, then print out each element in the array until reaching the last element in the array, in which case there are no more elements left."
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsI don't quite get what your question is, can you elaborate ?