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

very difficult to understand.. need more examples

I could not understand this lesson.. I wish and hope that you explain it in different way, and more examples . thx U in advance.

6 Answers

Bernard Paulet
Bernard Paulet
3,364 Points

The basic difference between While loop and Do..While loop is that : -While loop tests the condition before going through the loop block instructions

  • Do..While loop performs first the loop block of instructions and then tests the condition

the while loop will only work if the condition is true

var i = 0

while i=1 {

println("I looped")

i++

} \\it won't execute

However, the do while loop will do it once and then check if the condition is true

var i = 0

do {

println("I looped")

}while i=1

\\the statement will run once

So...the difference is that the do while loop will execute even when the condition isnt true but the while loop only executes if the condition is true

Bobby T
Bobby T
1,010 Points

It should be noted that:

while i = 1

needs to be changed to:

while i == 1

because while is always checking for a boolean True or False, and the = sign is an assignment operator while == is a comparison operator

the example are not working in Playground and while they do not working or print out nothing .. so I do not understand them why they are the way they are!!!!

what I can understand the difference between while and do, that while will not work in advance , but "do" will go through the code even if not work.. but what is the meaning if "do" will go through the code while it does NOT work !!!! no logic !

Petri, I don't think you understand. There is logic and a use for the do-while, for instance, lets say you wanted to make a game where you spun a wheel. It cost money to spin the wheel. However, every day you wanted to give someone a free spin.

var ifIHaveSpunToday = false

do {

spinWheel \you haven't covered functions yet so just pretend like you can say spin wheel and it will spin

ifIHaveSpunToday = True

} while == false

This way, the person will get to spin once if they havent spun yet, then the computer will know they have spun, and then the computer will check the condition to make sure it works-- but it won't, you have already spun.

And yes you could do this with a while loop...but this is just one example of a do while being better programming when needed

this code above did not work in my side !! in your answer your wrote:

do while loop will execute even when the condition isn't true

but your code below does not work in my Playground compiler

do { println("I looped") }while i=1 \the statement will run once

Sorry! I accidentally had my comparitave operator syntax wrong. I edited my previous post and now it will work. I did this on playground to helo you '''swift

var i = 5

while i == 0 {

println("I Looped")

i++

}

do {

println("I looped from the do")

} while i == 0 '''