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

Could someone please explain the while and do-loop with some examples in daily life for me to understand?

Eg. Using something like a car or tree or something to explain it for me to understand it better.

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Michael;

Welcome to Treehouse!

How about for a do... while loop for a car as something like, while dirty, wash car. Or for a tree while Maple, get syrup.

Basically in code you are using a do... while loop to set some condition and then have the program do something while that condition exists. So if you want to count to 100 you would say while not 100, keep counting. Or while the inputted password doesn't match, keep prompting for user input.

Make any sense?

Ken

Thanks very much. It does make sense now..

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Michael,

I will certainly do my best to explain.

Let's start with the while loop. While loops are used when the stopping point for the loop is undetermined. Compare this to other loops like 'for' where the number of times to loop is finite.

Here's an example. Say a car salesman wants to input new brands through our software and we have no idea how many will be added. In this case, the end to our loop is undetermined. We can use a while loop to ask for the users input until they hit a particular key and press enter. The ruby code below demonstrates this concept.

cars = []
car_name = nil

puts "Type -q to stop"
while car_name != '-q' do
    print "Enter car brand: "
    car_name = gets.chomp

    cars.push(car_name)
end

To understand the difference between the while and do-while loop, It's important to note that it's possible for a while loop to have a false condition and never execute the code within. Consider this modification to the code.

cars = []
car_brand = '-q'

puts "Type -q to stop"
while car_brand != '-q' do
    print "Enter car brand: "
    car_brand = gets.chomp

    cars.push(car_brand)
end

You'll notice that the only modification I've made is to set the car_name variable to the initial value of '-q'. This causes the while loop to be skipped immediately because the condition for the loop evaluated to false on the first iteration.

A do-while loop, however, will execute the code of your loop at least once, regardless of the condition.

cars = []
car_brand = '-q'

puts "Type -q to stop"
begin
    print "Enter car brand: "
    car_brand = gets.chomp

    cars.push(car_brand)
end while (car_brand != '-q')

When running this code, the terminal will ask for a brand even though we've set the variable to '-q' before the loop begins.

I hope this helps.