PHP PHP Arrays and Control Structures PHP Loops Do While Looping

gabriel wambua
gabriel wambua
5,058 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
201,931 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,058 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
201,931 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.