Break in a for loop
-
Calling break inside nested for loops breaks all loops or just the inner loop where break is found? Do I need to do this to exit all loops?
bool breakouter = false;
for(int x=0;x -
Calling break inside nested for loops breaks all loops or just the inner loop where break is found? Do I need to do this to exit all loops?
bool breakouter = false;
for(int x=0;xOnly the inner loop. If you want to break out of more loops you can do something like:
for(int x=0;x
Mircea -
Calling break inside nested for loops breaks all loops or just the inner loop where break is found? Do I need to do this to exit all loops?
bool breakouter = false;
for(int x=0;xProbably one of the last valid usecase for goto. Especially if more than 2 inner loops. Obviously, if your loops are doing many resources allocations, you'll need to do manual cleaning.
CI/CD = Continuous Impediment/Continuous Despair
-
Probably one of the last valid usecase for goto. Especially if more than 2 inner loops. Obviously, if your loops are doing many resources allocations, you'll need to do manual cleaning.
CI/CD = Continuous Impediment/Continuous Despair
Thank you guys
-
Probably one of the last valid usecase for goto. Especially if more than 2 inner loops. Obviously, if your loops are doing many resources allocations, you'll need to do manual cleaning.
CI/CD = Continuous Impediment/Continuous Despair
-
Probably one of the last valid usecase for goto. Especially if more than 2 inner loops. Obviously, if your loops are doing many resources allocations, you'll need to do manual cleaning.
CI/CD = Continuous Impediment/Continuous Despair
Maximilien wrote:
Probably one of the last valid usecase for goto. Especially if more than 2 inner loops.
There were lots of valid 'goto' use cases cases, before for loops, while loops, and arbitrarily nested if-else, were invented. Structured exit from multiply nested loops were invented 40+ years ago. In 1980 vintage CHILL, you could do
outermost: intermediate: inner: exit intermediate;
exit intermediate; would skip the "more intermediate statements" as well as further intermediate iterations, going directly to "more outermost statements" and continue with further outermost iterations. If it rather said exit outermost;, the "more outermost statements" would be skipped as well, and the outermost loop would be left. exit would be a short form of exit innermost; This construct never made it into mainstream language such as the C derived ones. One partial explanation is differences in label semantics: In CHILL, a label identifies a block, not a point in the code. Furthermore, a statement is a block; it need not be embraced to be labeled. I would be willing to trade a lot of other 'improvements' in more recent language extensions in favor of something like this. Some concepts were not well developed in 1980. If a language of today were to introduce such a multi-level exit, it would probably come with something 'finally'-like mechanisms to do required cleanup operations regardless of loop exit method.
Religious freedom is the freedom to say that two plus two make five.
-
Maximilien wrote:
Probably one of the last valid usecase for goto. Especially if more than 2 inner loops.
There were lots of valid 'goto' use cases cases, before for loops, while loops, and arbitrarily nested if-else, were invented. Structured exit from multiply nested loops were invented 40+ years ago. In 1980 vintage CHILL, you could do
outermost: intermediate: inner: exit intermediate;
exit intermediate; would skip the "more intermediate statements" as well as further intermediate iterations, going directly to "more outermost statements" and continue with further outermost iterations. If it rather said exit outermost;, the "more outermost statements" would be skipped as well, and the outermost loop would be left. exit would be a short form of exit innermost; This construct never made it into mainstream language such as the C derived ones. One partial explanation is differences in label semantics: In CHILL, a label identifies a block, not a point in the code. Furthermore, a statement is a block; it need not be embraced to be labeled. I would be willing to trade a lot of other 'improvements' in more recent language extensions in favor of something like this. Some concepts were not well developed in 1980. If a language of today were to introduce such a multi-level exit, it would probably come with something 'finally'-like mechanisms to do required cleanup operations regardless of loop exit method.
Religious freedom is the freedom to say that two plus two make five.
The proprietary language in which I worked for many years had exactly what you describe. A label was for a
BLOCK
/ENDBLOCK
that you couldEXIT
. A label could also precedeIF
,DO
, and the equivalents ofswitch
) without using theBLOCK
keyword. The language was designed in the late 70s. This was in the telecom sector, so they probably took the design from CHILL.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Calling break inside nested for loops breaks all loops or just the inner loop where break is found? Do I need to do this to exit all loops?
bool breakouter = false;
for(int x=0;xWhenever I have such questions and I have them often I experiment w/ brief simple code and observe the behavior.