If I use a break
statement, it will only break inner loop and I need to use some flag to break the outer loop. But if there are many nested loops, the code will
A different approach is to refactor the code from two for loops into a for loop and one manual loop. That way the break in the manual loop applies to the outer loop. I used this once in a Gauss-Jordan Elimination which required three nested loops to process.
for (int i = 0; i < 1000; i++)
{
int j = 0;
MANUAL_LOOP:;
if (j < 1000)
{
if (condition)
{
break;
}
j++;
goto MANUAL_LOOP;
}
}