I am a beginner in Java so maybe I got this wrong, but do the labeled continue and break statements act like the goto of so many other programming languages?
If it does, should we ever use these labeled forms?
Printable View
I am a beginner in Java so maybe I got this wrong, but do the labeled continue and break statements act like the goto of so many other programming languages?
If it does, should we ever use these labeled forms?
The continue and break statements are normally refered to as transfer statments with the others being control if, if else and iteration for, while. The break statement serves to break loops and continue statements serve to continue execution by passing the point after the continue and going back to the begining of the loop. Goto should never really be used in any modern programming lauguage since it leads to what some people call Spaghetti code.
Labeled break and continue serve to break out of nested loops, like in
Whether it makes sense is open for debate.Code:outer:
for(int i = 0; i < 100; ++i) {
for(int j = 0; j <= i; ++j) {
break outer;
}
}