What are some better ways to avoid the do-while(false); hack in Java?
do-while, java
Solution
What is this "while(false)" nonsence? Use a label!
myLabel: {
if(!check()) break myLabel;
...
...
if(!check()) break myLabel;
...
...
if(!check()) break myLabel;
...
}
It's core Java: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
Problem
When the code flow is like this: ``` if(check()) { ... ... if(check()) { ... ... if(check()) { ... ... } } } ``` I have generally seen this work around to avoid this messy code flow: ``` do { if(!check()) break; ... ... if(!check()) break; ... ... if(!check()) break; ... ... } while(false); ``` What are some better ways that avoid this workaround/hack so that it becomes a higher-level (industry level) code? Are there maybe constructs that come from Apache commons or Google Guava? Note: this is a copy of the same question for C++. The best answers there are truly functions pointers and the GOTO command. Both doesn't exist in Java. I am eagerly interested in the same thing for Java. Putting it into a new function and using return is in my opinion not a good solution, because return quits the method. So if my class has 20 methods with these constructs, I would have to add 20 additional methods to get this done. That's why GOTO was the best answer for C++.