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

Question About Loops

The quiz asks this:

How many times will the following loop execute?

int i = 1;
do {
printf("looping");
}
while ( i < 1 ) ;

If i = 1 and 1 is not greater than 1 then why is the answer Once? Wouldn't it not execute because 1 isn't greater than 1? Or does it execute once no matter what?

3 Answers

A do-while loop always executes at least once before checking the condition, so even though i > 1, it executes that first time. That's just how do-while loops work. If you don't want it to execute that first time, you'd use a while loop:

while (i < 1) {
  ...
}

Hi Jason,

Yes, a do/while loop will always execute at least one time because the loop condition is evaluated at the end of the loop.

If that was a while loop then it would not loop at all.

Thanks guys :)