Some time ago, I came across the following construction, which I have rarely seen since, although I use it relatively often. I usually use it when checking the whole list of conditions is correct and prevents large levels of indentation. Essentially, it uses a for loop to create a kind of structured goto. My question first of all is whether there is a better way to structure this, secondly, whether people will like it, and thirdly, whether there will be a new keyword in java / C ++, etc., Such as unit { }, which will cause only breaks to exit to the end of the device, will be useful and understandable.
ps I understand that he is slipping away from an endless cycle, but I think that my paranoia about it means that he never was.
Edit: I added a setup code for further conditions, in order to try to highlight the problems with a chain, if then elses
boolean valid = false; // this loop never loops for (;;) { if (!condition1) break; condition2.setup(); if (!condition2) break; condition3.setup(); if (!condition3) break; valid = true; break; } if (valid) dosomething();
EDIT:
I just discovered that there really is a way to structure this in java without misusing loops, etc. and wondered if it looked like he would frown, although I think I missed the boat on this one.
The modified code is as follows.
boolean valid = false; breakout: { if (!condition1) break breakout; condition2.setup(); if (!condition2) break breakout; condition3.setup(); if (!condition3) break breakout; valid = true; } if (valid) dosomething();
Now that the misuse of the for loop, which has caused a lot of complaints, is removed, and in fact this is a solution that I think is pretty neat and that is exactly what I was looking for. I guess this structure is probably not very well known, since no one mentioned it, do people object to it so much?