Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
215,256 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
215,256 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.