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 Collections and Control Flow Control Flow With Loops Working With Loops

Sean Lafferty
Sean Lafferty
3,029 Points

I really dont get this loop stuff, can someone please help

I just dont get it :(

loops.swift
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below

while counter <= numbers.count() {

numbers >counter

}

2 Answers

Hi Sean,

We're iterating over the numbers array. Eventually we'll be accessing that array and adding the numbers up. For now we just want to create the loop.

The question tells us to use a while loop and to keep looping while counter is less than the number of elements in the numbers array. You've pretty much got that done - you need to use less than, not less than and equals to (because arrays start counting at zero). So, you've got this:

while counter < numbers.count {
  // do something
}

This, however will run forever. That's because counter is set to zero and we're not changing its value inside the loop. We want to increment it. To do that, add 1 to it; do this inside the loop:

counter = counter + 1
// or use +=
counter += 1

That way, at some point counter won't be less than numbers.count and the loop will terminate.

The skeleton loop looks like:

while counter < numbers.count {
  counter += 1 // one of two options for this
}

Make sense?

Steve.

P.S. edited - forgot ++ was removed from Swift 3 - oops!!

Sean Lafferty
Sean Lafferty
3,029 Points

Thanks Steve, sorry for the constant questions! I'm really struggling to understand the arrays/dictionaries/sets portion of this course. i think im probably going to repeat the module and do some research as its going to be a fundamental part of swift and I dont want to just copy paste and not really grasp how these things work! Thankyou very much for your help!

Sean

Don't apologise. Keep asking q's, I'll try to answer as best I can!

Ezra Siton
Ezra Siton
12,644 Points
while (condition) {
        code block to be executed
}

The example below output (1 2 3 4 5)

var i = 0;
while (i < 5) {
    i += 1; //increment i so eventually we break out of the while loop
    print(i);  
}

About your code: ** 1.You dont have counter **increment**

Infinitely loop is a while loop that never ends, because the condition is always true

  • 2. You have syntax error. Dont add for .count*()* but .count Throw error > Swift:: Error: cannot call value of non-function type 'Int'

Summary. (fixing A+B)

let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below
while counter < numbers.count { 
     counter += 1;
}