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

Better understanding

I am not sure what to do. I really don't understand why this is important and how to do it. Could someone help or clarify it better?

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

2 Answers

Richard Lu
Richard Lu
20,185 Points

Hi Joshua,

The challenge is to print out each number in the given array. In order to do that, you'll need a counter variable. Here's a demonstration of how I solved this problem.

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

var counter = 0   // the counter variable
while counter < numbers.count {  // while number is less than the length of the array enter this loop
  println(numbers[counter])
  // in this case the last loops counter will be 11 (greater than the length of the array) and will not be able to enter the loop again
  counter = counter + 1   // increase the counter by 1
}

Good luck!

Thank you. How can this be used for making apps?

Richard Lu
Richard Lu
20,185 Points

Hey Joshua,

This is a tiny part of the puzzle of building an app. Keep following the track and it'll teach you.

Happy Coding! Good luck :)