Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Basics (retired) Control Flow While and Do-While Loop

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

I tried multiple different lines of code but cant get it to work

Logan R
Logan R
22,989 Points

Can you please post your code?

Try to use proper mark-up. Put three ` before and after your code.

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] var index = 1 while index = number { println(numbers[index]) index++

6 Answers

Logan R
Logan R
22,989 Points

You are using the while loop wrong. The rest is fine. You want to loop to run as long as the index is smaller than the total number of numbers in the array.

Count how many numbers are in the array by using .count (IE: todo.count).

(Use < instead of =)

Thank You for the help :)

Logan R
Logan R
22,989 Points

No problem! If you still can't get it to work, come back and post your code here and I will see if I can help you more :)

ok got a new problem :(

I thunk i added what you said and the error that came up was that I needed to use println() but I did so think I just got this all muddled up :/

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers.count
var index = 1
while index < numbers.count {
println(numbers[index])
index++ }
Logan R
Logan R
22,989 Points

You need to start your index at 0. When you count numbers, most people count: 1, 2, 3, 4...etc but most programming languages, including Swift, start counting at 0: 0, 1, 2, 3...etc.

Thankyou. That did the trick :)

McKenna Rowe
McKenna Rowe
1,869 Points

Like Zak, I'm wondering why you had to add numbers.count after the array? That didn't come up during the lesson. Is that "setting the count" in a sense?

Maher Alhasan
Maher Alhasan
974 Points

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

Gaston Trussi
Gaston Trussi
528 Points

numbers.count is not necessary in the second line.

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

var index = 0

while index < numbers.count {

println(numbers[index])

index++ 

}
Zak Lewis
Zak Lewis
6,514 Points

Logan is right, it should look like this:

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

Zak Lewis why did you add

numbers.count 

after the array?

also shouldn't index be a constant instead of a variable since the numbers array is a constant?

tomkosina
tomkosina
16,232 Points

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