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 Do While Looping

gabriel wambua
gabriel wambua
5,061 Points

Hi this is a question with regards to loops specifically while loops

Hi this is a question with regards to loops. I just realized that if I forget to do a ++ incremental in a while loop it will go on forever during run time.

My question is why is the incremental bit important?

1 Answer

Steven Parker
Steven Parker
230,688 Points

The loop runs until the condition being tested in the "while" becomes false. If nothing inside the loop changes the condition, it will never stop.

I assume the increment you refer to was being applied to a variable that is being tested in the "while" expression.

gabriel wambua
gabriel wambua
5,061 Points

I agree...but why is it important to place the ++ signs after the loop has run? Which real world scenario would this be relevant for? I an just trying to wrap my head around this.

Steven Parker
Steven Parker
230,688 Points

The increment happens each time through the loop, and the value eventually becomes larger than what it is being compared to and the loop stops.