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

Carlo Fox
Carlo Fox
298 Points

While Loops Questions

I'm stuck on this challenge.

I believe that I am to use the formula below except that I am to replace the todo.count and todo[index] with data from the challenge question. Possible to walk me though this challenge? Thanks!

var index = 0

while index < todo.count { print(todo[index]) index++ }

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

1 Answer

Richard Lu
Richard Lu
20,185 Points

Hey Carlo,

The snippets of code below will walk you through how this is done.

As you mentioned, you want to use the same steps used in a previous example.

var index = 0
while index < todo.count { 
   print(todo[index]) 
   index++ 
}

In that example, you had an array named todo, but for this challenge the array is named numbers. So all you have to do is replace todo with numbers.

var index = 0   // the index represents the position of the array currently
while index < numbers.count {   // if the position is outside of the array stop
   print(numbers[index]) 
   index++    // go to the next position of this array
}

Happy coding! :)