'While' inner loop in 'For' outer loop. Why doesn't this work?

I would like an inner (nested) while loop to break when a condition is met, and continue an outer (parent) for loop. I have created a simplified example:

<?php
$teams = array(
    0 => array('points' => 4),
    1 => array('points' => 1),
    2 => array('points' => 3),
    3 => array('points' => 8),
    4 => array('points' => 2),
    5 => array('points' => 6)
);
for($t = 1; $t < 6; $t++){
    $break = 0;
    echo "Team: " . $t . " ";
    while($break == 0){
        // The real case does complex calculations instead of the following line
        echo "Points: " . $teams[$t]['points'] . "<br>";
        if ($teams[$t]['points'] == 8){
            $break = 1;
        }
    }
}
?>

Why does it fail? In my mind it should output:

Team: 1 Points: 1
Team: 2 Points: 3
Team: 3 Points: 8
Team: 4
Team: 5

What is the actual point on the while loop. You can use a break statement to break out of a loop. But based on what you are showing, it should be while(true), but what you are telling says that isn’t the case.

Thanks for the reply. In the real case, the outer for loop is for looping through football gameweeks in a season.

The point of the inner while loop is to minimize a root mean squared error (RMSE) between predicted points and actual points for that gameweek. To achieve that, it repeatedly increments/decrements a weighting (variable) and recalculates the RMSE. When the error reaches its minimum and starts increasing, the weighting value reverses by one increment/decrement and the inner loop should break out to continue with the next gameweek in the parent for loop (proceeding to the inner while loop again to adjusting the weighting to minimize the RMSE for that gameweek etc. etc.).

Should I post the whole thing? It’s pretty long.

I’ll have to check in later when my head isn’t swimming, I have too much to think about at work this minute, but didn’t want to make this hidden for reading but not responding.

1 Like

Had a break point between meetings!

Make this change and test it out.

do {        
    // your processing code
} while ($teams[$t]['points'] == 8);

However, if you have so much code that it is a lot to post, you need to break it up into functions to be called within the loop.

Thanks. Unfortunately the do-while loop doesn’t help. I tried it with != 8 as well as your == 8.

I’ve tried so many different things now. It just seems there’s no way to continue with an outer for loop after exiting an inner loop that does calculations and tests for a condition. It’s baffling. I suppose it might be because of the use of a variable ($t in the example) to address an array index.

Example

Thanks for the example. It spurred me on to try some more things, and I’ve now got my inner loop working and breaking correctly, for both my real project and the simplified project. However I cannot find a way to make the while loop “endless-until-broken”. I have to put an arbitrary limit of 20 on it, and that is a workable solution, but not ideal.

So my question has now changed somewhat… Looking at the following code, is there a way for to do away with the arbitrary while limit of 20, and make it endless-until-broken?

$teams = array(
    0 => array('points' => 4),
    1 => array('points' => 1),
    2 => array('points' => 3),
    3 => array('points' => 8),
    4 => array('points' => 2),
    5 => array('points' => 6)
);
for($t = 1; $t < 6; $t++){
    $i = 0;
    // How to make the loop below be endless-until-broken, instead of having an arbitrary limit of 20 loops?
    while($i < 20){
        echo $teams[$t]['points'];
        if($teams[$t]['points'] == 8) break;
        $i++;
    }
}

Unfortunately while(true) doesn’t work on my inner loop.

I don’t understand how it wouldn’t work…

Me too :cry:

It’s highly likely that my simplified examples don’t really make sense, or that I’m completely misunderstanding the whole issue.

Anyway, my example above with the non-endless while($i < 20) loop is getting the job done (inelegantly), so to be pragmatic I’ll use it and move on. If in the future I find a solution, or have a better understanding of breaking nested loops, I’ll try to remember to return to this post. Thanks again.

Sponsor our Newsletter | Privacy Policy | Terms of Service