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 (retired) Control Flow While and Do-While Loop

kirstie perez
kirstie perez
312 Points

While loop not working. Please help.

Guys, this code isn't working for some reason. Please answer if you see anything wrong, cause' i can't see anything wrong. Thanks! :)

while_loops.swift
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var index = 0
while index < numbers.count{
    println(numbers.count[index])
    ++index
}   

4 Answers

Dennis Parussini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dennis Parussini
Treehouse Project Reviewer

Yes, it should be index++. ++index increments the index before the check for index < numbers.count. This way your array would be out of range.

Hello,

I believe it should be index++.

Also can I see the error?

Like the instructor said: when you write ++index in a loop that starts conditioning the "index" value to be less than numbers.count, there is a point where index value = 10 and then turns into 11 inside the loop... which is not supposed to happen since 11 is not lower than numbers.count..

English is not my main language so i apologize if my answer was not clear.

kirstie perez
kirstie perez
312 Points

Thanks to all who answered. You helped a lot.

No problem