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

Josh Warden
Josh Warden
616 Points

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

I can't figure out how to do this?

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

This means the challenge wants you to iterate through the elements (items) of the array, and print them. Iterating an array means to go through every element of an array and doing something. In this case, printing.

4 Answers

hey josh!

the answer Jari gave is 50% correct answer because if you will try that code you won't get output what they have asked!

here is the correct one :

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

var i = 0

while i < numbers.count {

println("\(numbers[i])")

i++

}

Jari Koopman
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jari Koopman
Python Web Development Techdegree Graduate 29,349 Points

the code could be something like:

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

I hope you can go on with your course! I don't exactly know how far you are in your course so this answer can be a bit confusing. I hope it's not!

Jari, yours looks good, except we were taught to do it, speaking on the 5th line, with just "i++" in your case. Our examples just used the word index, which I assume is what i stands for. So with "index" in the variable you type "index++" on line 5. And always after the print statement!

Malerie Anderson
Malerie Anderson
4,941 Points

Hi Josh!

It would be helpful to know which part you're stuck on specifically. It's hard to know what question to answer when you just say that you don't know how to do it. I'd recommend re-watching the videos if you aren't sure where to start and including your code in the question so we can help you figure out what's not working for you. :)

Josh Warden
Josh Warden
616 Points

Thank you for your help