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 trialAnna Isabel Lamers
Courses Plus Student 331 PointsMy while and do-while loops each perform the function twice as often as the number of items in my array. Why is that?
var todo : [String] = ["Learn with TreeHouse", "Prepare meals for Take Out", "Read Book", "Go to Work"]
for item in todo{println(item)}
var index = 0 while index < todo.count {println(todo[index]);index++}
I use the exact same code, just having 4 items (instead of 5 as in the example given) and my panel indicates that the function is executed 8 times.
I cannot seem to make sense of why it does not say 4 times (once per item of the array).
Can anyone help?
1 Answer
miguelcastro2
Courses Plus Student 6,573 PointsBecause you have two loops running back to back, each loop has 4 items (4+4 = 8). To illustrate this just remove the "for in" loop.