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

Tim Hancock
Tim Hancock
3,254 Points

Why does my playground say I can't use a do-while? It says to use a repeat-while?

Can anybody answer this? I don't want to proceed without knowing if I'm doing something wrong.

var todo : [String] = ["Return calls","Write blog","Cook Dinner"]

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

index = 0

do { print(todo[index]) index++ } while index < todo.count

I'm following the lesson but my playground won't let me use the do-while. Has anybody experienced this?

Randy Mitchell
Randy Mitchell
2,127 Points

Do-While is no longer valid syntax and has been replaced with Repeat-While.

e.g.

var todo = ["Practice Coding", "Return Calls", "Cook Dinner"]

var index = 0

repeat {
  print(todo[index])
  index += 1
} while index < todo.count

PS: ++ and -- have also been deprecated. Incrementing and Decrementing are now done with += and -= respectively

2 Answers

Tim,

The following works fine for me:

var todo : [String] = ["Return calls","Write blog","Cook Dinner"]

var index = 0

while index < todo.count {

    print(todo[index])
    index++
}

Try adding this to your playground. Let me know if it works or not.

Randy Mitchell
Randy Mitchell
2,127 Points

Tim, he was trying to implement Do-While loop. The example above is just a While loop. Do-While is no longer valid syntax in Xcode 7.3.1 or apparently any versions beyond the the instructors.

Do-While has been replaced with Repeat-While.

e.g.

var todo = ["Practice Coding", "Return Calls", "Cook Dinner"]

var index = 0

repeat {
  print(todo[index])
  index += 1
} while index < todo.count

PS: ++ and -- have also been deprecated. Incrementing and Decrementing are now done with += and -= respectively

Michael Brown
Michael Brown
12,406 Points

Hey Tim, this is because you're running an upgraded version on Xcode 2.3. During this video, they were on 2.1 which had them using do-while. Xcode 2.3 has developers use repeat-while instead which does the same thing.