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

What expression is this operator expecting?

On the challenge task for While and Do-While Loop, we're asked to create a while loop that prints out the array. Keep getting an expected expression operator after closing curly brace. Since there's only 1 set of curly braces, what else is missing?

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

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Jaytova,

I'd be glad to help.

The issue I see in your first example is that the index variable is set to a constant using keyword "let", which cannot be updated after an initial value has been set. The reason the second example works is because the index variable is declared using "var".

I also see that in your example you have the "++" after a space. It must be together like "index++".

It should look like this

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 this helps.

Good catch man! Thanks!

Ok figured this out. The closing curly braces needs to be on the same line as the increment. Not sure why? In Amit Bijlani example, he puts the closing curly braces on the following line:

var todo : [String] = ["Return Calls","Write Blog","Cook Dinner","Pickup Laundry","Buy bulbs"]


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

Justin Horner or Kai Aldag can you explain?

Also I noticed some users put

numbers.count

right after the array. It compiles without that function. Why would you add that?

And I made my index a constant to match the array. Everyone else put

var index = 0

Does index need to be a variable or should the array define the index?