Re: Using "break" to Break Out of Multiple Loops in One Call



On Fri, 15 Feb 2008 16:30:01 -0800, David Anton <DavidAnton@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

I've always considered Java 'labelled breaks' as just a way for the Java
designers to get 'goto' into Java without admitting defeat.

I agree with you, sort of.

Especially when
you consider that you can use the labelled break to jump out of from any
braced block, not just loops.

The main difference I see is that the syntax of the Java labeled break reinforces the idea of a structured flow. It's true that you can add braces to force an equivalence to a goto, but when you do so the language is (if you're paying attention) giving you very loud clues about whether or not that's an appropriate use of the "break-as-goto".

Personally, I'm not of the mind that "goto" statements are inherently bad. They certainly can be misused, and may well be misused much more than other language constructs. But they also have their place (and I don't just mean as a replacement for falling through in "case" statements :) ). I have found that, as I explore Java and implement real code using it, that in a situation where I might have used a "goto", using a labeled "break" provides similar functionality, but does so in a way that I think makes the structure of the code clearer.

I prefer to believe that I never misuse a "goto" statement, but whether I do or not, it's my belief that the Java syntax would make it a lot more obvious to me if I'd abused the statement.

So, I'm not sure that in Java it was so much a matter of whether they were admitting defeat or not, as it is a matter of trying to provide a slightly different syntax to do the same thing, but in a more clear way that avoids code maintenance issues that misuse of gotos can cause.

Granted, I don't know what the Java designers were actually thinking, but I like to think that's at least part of why it's the way it is.

Of course, it begs the question: why isn't a similar syntax in C#? After all, my after-the-fact impression of C#, having made the transition backwards into Java from C#, is that C# seems to try to take what was best of Java, while avoiding some of the more confusing, redundant, or restrictive parts. Inasmuch as I personally find the labeled "break" statements somewhat clearer than a "goto" statement, that doesn't exactly fit into the impression I've formed.

But then, again, that's just my impression. I don't really know all of the motivations behind the language design choices, and maybe they felt they had a good reason to return back to the conventional "goto" statement.

Either way, I don't think there's anything inherently bad about labeled "break" statements or "goto" statements. Sometimes they are just what you need. Just remember not to aim at your foot when using them. :)

Pete
.



Relevant Pages

  • Re: Would like a preprocessor.
    ... > This will probably blown your mind but I would kill for a GOTO ... whay are you writing in Java in the first place? ... > decided to try straight java first. ...
    (comp.lang.java.programmer)
  • Re: Would like a preprocessor.
    ... >> This will probably blown your mind but I would kill for a GOTO ... > I hate to tell you this, but Java actually does have a GOTO (not under ... >> I considered writing this class in JASMINE the bytecode assembler but ... >> decided to try straight java first. ...
    (comp.lang.java.programmer)
  • Re: Shall I move from C to Java?
    ... You have to analyze what it means to use Java ... another question is how to disable "goto", ... Need to learn how to apply control theory in your embedded system? ... "Applied Control Theory for Embedded Systems" by Tim Wescott ...
    (comp.arch.embedded)
  • Re: Structured Coding
    ... Standard Pascal does include goto, ... I like this quote from _Learning Java_: ... Microsoft is changing the language and even more so the APIs (and the ... it's not a proper introduction to programming, ...
    (comp.lang.cobol)
  • Re: VOTE
    ... common way of coding the procedure division. ... I think it's also used in fortran as well. ... think Java, but if you use goto in your C program, your colleagues are liable to break your arms and knee-caps, knock out your teeth and poke out your eyes with a corkscrew and actually get away with it on grounds of justifiable self defence. ...
    (Fedora)