Properties of a while loop
Posted: Tue Apr 04, 2006 7:06 pm
I had a little bug in some code I wrote, I ended up tracking it down to a while loop. Are while loops supposed to stop executing when a condition is no longer true, even if it hasn't even completed the loop yet?
Here is an example:
If $Record[1] doesn't exist, this code will stop executing and break out of the loop at line 2 [ie line 3 will never be executed], and the results won't be correct. If I switch lines 2 and 3, it works fine.
Here is an example:
Code: Select all
while (isset($Record[$RecordNumber])) {
$RecordNumber++; // line 2
$TotalRating = $TotalRating + $Record[$RecordNumber]['Rating']; // line 3
}