Hi everybody.
If I have nested loops, and I'd like to exit all of them given a condition,
How do I break out?
I don't know if C has got some exit or anything that suites the case,Code:for (x = 0; x < lim0; x++){ for (x1 = 0; x1 < lim1; x1++){ for (x2 = 0; x2 < lim2; x2++){ if (contidion) break; // this exit the inner loop only - } } }
the only alternatives I see at the moment are return if I put the nested
loops in a function, goto that is badly considered in modern world
or to change the whole structure of the program, and that could be quite heavy
to digest.
Or I could use a workaround:
Code:for (x = 0; x < lim0; x++){ for (x1 = 0; x1 < lim1; x1++){ for (x2 = 0; x2 < lim2; x2++){ if (contidion){ break_x1 = 1; break_x = 1; break; // this exit the inner loop only } } if (break_x1) break; } if (break_x) break; }
Any alternative in standard C99?