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

PHP PHP Arrays and Control Structures PHP Loops While Quizzing

The answer to this question is confusing.

What would be displayed when the following code is executed?

$i = 0; do { echo "$i is greater than 2\n"; $i++; } while ($i > 2);

The correct answer was 0 is greater than 2 Why does this not keep looping until $i becomes 2?

I think I figured it out. It is this way because the condition is not met it will not run. 1 is not greater than 2. For the loop to run the condition $i is greater than 2 must be true. The only reason it runs is that there is a "do" loop/condition before the "while" loop/condition. The "do" condition will make something happen at least once before checking to see if the "while" condition is true. Is that correct?

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

HI Kathryn,

Yep. You're right on! :thumbsup: A do/while loop will always execute once before the condition is even checked.

Keep Coding! :) :dizzy:

Thank you, Jason!

Maybe you can help me to solve this complicated logical operator dilemma. https://teamtreehouse.com/community/ping-pong-logical-operators