Is there a goto statement (or something similar)?

Discussion in 'Java' started by Mike42, Nov 12, 2005.

  1. Mike42

    Mike42 Guest

    Is there a goto statement or something similar in java? If so, how do I
    use it?
    Mike42, Nov 12, 2005
    #1
    1. Advertising

  2. Mike42

    Stefan Ram Guest

    Stefan Ram, Nov 12, 2005
    #2
    1. Advertising

  3. Mike42

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >The statements "switch" or "throw" do something similar.


    "switch" and "throw" are not statements, but keywords.

    A more appropriate wording might be
    "The switch- and the throw-statement do something similar."
    Stefan Ram, Nov 12, 2005
    #3
  4. "Stefan Ram" <-berlin.de> wrote in message
    news:-berlin.de...
    > -berlin.de (Stefan Ram) writes:
    >>The statements "switch" or "throw" do something similar.

    >
    > "switch" and "throw" are not statements, but keywords.
    >
    > A more appropriate wording might be
    > "The switch- and the throw-statement do something similar."


    ??

    Your sentence version is virtually identical.

    Regardless, it is a pointless argument of semantics over an exceptionally
    simple question.

    --
    LTP

    :)
    Luc The Perverse, Nov 12, 2005
    #4
  5. Mike42

    Roedy Green Guest

    On Sat, 12 Nov 2005 17:05:43 -0500, "Mike42"
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Is there a goto statement or something similar in java? If so, how do I
    >use it?


    Break. It lets you leap out of a loop or loop nest.

    See http://mindprod.com/jgloss/jcheat.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 13, 2005
    #5
  6. Mike42 wrote:
    > Is there a goto statement or something similar in java? If so, how do I
    > use it?


    There is no goto per se (though the the keyword is reserved), but break
    and continue statements cause unconditional branching in more
    restrictive contexts. You can combine them with statement labels to
    achieve many of the effects that it is reasonable to want goto for. As
    Stefan Ram pointed out, a switch block or a throw statement also produce
    branching. So does a try/catch/finally, an if, a for or while loop, or
    a method invocation.

    --
    John Bollinger
    John C. Bollinger, Nov 13, 2005
    #6
  7. Mike42

    Roedy Green Guest

    On Sat, 12 Nov 2005 17:05:43 -0500, "Mike42"
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Is there a goto statement or something similar in java? If so, how do I
    >use it?


    In the JVM of course there are jumps and conditional jumps. Part of
    what the byte code verifier does is make sure the stack is consistent
    no matter which way you jump, and to make sure you don't jump into the
    middle of an instruction or outside the method.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 13, 2005
    #7
  8. Mike42

    Rhino Guest

    "Mike42" <> wrote in message
    news:...
    > Is there a goto statement or something similar in java? If so, how do I
    > use it?
    >

    At the risk of sounding like an old geezer, every programming course I have
    taken in over 20 years of professional programming has strongly discouraged
    the use of 'goto' statements. Every experienced programmer I have met in
    that time has had the same view, based on their own experiences with the
    unmaintainable mess that results: 'goto' statements are evil.

    Luckily, Java doesn't have a 'goto' but, even if it did, you should avoid it
    like the plague. Ditto for 'goto' in any other programming language.

    Rhino
    Rhino, Nov 13, 2005
    #8
  9. Mike42

    Chris Uppal Guest

    Rhino wrote:

    > At the risk of sounding like an old geezer, every programming course I
    > have taken in over 20 years of professional programming has strongly
    > discouraged the use of 'goto' statements. Every experienced programmer I
    > have met in that time has had the same view,


    Clearly we have not met ;-)


    > based on their own
    > experiences with the unmaintainable mess that results: 'goto' statements
    > are evil.


    Interestingly, the C++ source for the native part of the AWT implementation
    makes heavy use of goto in order to /improve/ the clarity, structure, and
    maintanability of the code. Works too...

    -- chris
    Chris Uppal, Nov 13, 2005
    #9
  10. Mike42

    Tony O'Bryan Guest

    Mike42 wrote:

    > Is there a goto statement or something similar in java? If so, how do I
    > use it?


    The goto statement is a programming anachronism with almost no surviving
    use. The one justifiable use I've seen is to cleanly escape deeply nested
    conditionals within a function.

    =========================
    if (...)
    {
    if (...)
    {
    if (...)
    {
    goto handler;
    }
    }
    }
    return;
    handler:
    cleanup;
    =========================

    However, try/catch blocks are a better substitute:

    =========================
    try
    {
    if (...)
    {
    if (...)
    {
    if (...)
    {
    throw new SomeExceptionType();
    }
    }
    }
    }
    catch (SomeExceptionType e)
    {
    }
    =========================

    At its most basic level, this construct works similarly to a goto in this
    case. Exceptions are much more powerful, though, in that unhandled
    exceptions will propagate up the call stack in a predictable manner until
    something else handles them.

    This allows an escape from deeply nested conditionals to travel through the
    method as well as to outside the method, but in a clean fashion that
    doesn't require any knowledge of anything outside the method. Your method
    merely has to declare that it doesn't handle whatever exception type it
    wants to pass up the stack.

    =======================
    public void myMethod() throws SomeUnhandledException
    {
    }
    =======================
    Tony O'Bryan, Nov 13, 2005
    #10
  11. Mike42

    Roedy Green Guest

    On Sun, 13 Nov 2005 14:38:18 -0000, "Chris Uppal"
    <-THIS.org> wrote, quoted or indirectly
    quoted someone who said :

    >Interestingly, the C++ source for the native part of the AWT implementation
    >makes heavy use of goto in order to /improve/ the clarity, structure, and
    >maintanability of the code. Works too...


    Is it mechanically generated code?
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 13, 2005
    #11
  12. Mike42

    Roedy Green Guest

    On Sun, 13 Nov 2005 14:55:22 GMT, Tony O'Bryan
    <> wrote, quoted or indirectly quoted someone
    who said :

    >try
    > {
    > if (...)
    > {
    > if (...)
    > {
    > if (...)
    > {
    > throw new SomeExceptionType();
    > }
    > }
    > }
    > }
    >catch (SomeExceptionType e)
    > {
    > }


    you can also do that with:

    outer:
    > {
    > if (...)
    > {
    > if (...)
    > {
    > if (...)
    > {

    break outer;
    > }
    > }
    > }
    > }

    // continues here
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 13, 2005
    #12
  13. Mike42

    Rhino Guest

    "Chris Uppal" <-THIS.org> wrote in message
    news:43774fe8$0$38041$...
    > Rhino wrote:
    >
    > > At the risk of sounding like an old geezer, every programming course I
    > > have taken in over 20 years of professional programming has strongly
    > > discouraged the use of 'goto' statements. Every experienced programmer I
    > > have met in that time has had the same view,

    >
    > Clearly we have not met ;-)
    >
    >
    > > based on their own
    > > experiences with the unmaintainable mess that results: 'goto' statements
    > > are evil.

    >
    > Interestingly, the C++ source for the native part of the AWT

    implementation
    > makes heavy use of goto in order to /improve/ the clarity, structure, and
    > maintanability of the code. Works too...
    >

    I like to think I'm an open-minded guy so I'm willing to concede that 'goto'
    might possibly be a worthwhile construct _if used correctly in a limited
    number of circumstances_ like the ones that you suggest.

    Honestly, I've never much thought about using 'goto' because of all the
    anti-goto propaganda I've heard from Day One of my professional programming
    days.

    I *think* the anti-goto advice is primarily a reaction by structured
    programmers to the preceding era of programming. My impression is that the
    previous less-sophisticated philosophy of programming valued conciseness of
    code above all so many people wrote short blocks of code and then used goto
    statements to branch to them, wherever they were, to keep programs small.
    The result soon became known - disparagingly - as spaghetti code. The "new"
    generation of structured programmers turned up their noses at spaghetti code
    and decreed the 'goto' statement the chief culprit and insisted that it
    never be used. (Actually, in our COBOL shop, 'goto' *was* permitted for one
    special case: to go to the last section in the paragraph, which invariably
    contained a single 'exit' statement.)

    So maybe the taboo against 'goto' is a bit dated. Perhaps it would be fair
    to say that the goto is a reasonable statement to use for certain
    circumstances and then carefully enumerate those cases. But I am so used to
    avoiding 'goto' that I doubt I'd ever start using it, even if Java had a
    'goto' statement; I'm far more used to accomplishing what I want without
    'goto'. Maybe I *am* a geezer, like the people who declined to use cars
    because their old horses worked just fine :)

    Rhino
    Rhino, Nov 13, 2005
    #13
  14. Roedy Green <> writes:

    > outer:
    > > {


    break/continue only work with loop labels, so you need to make a dummy
    one around the block, e.g.

    outer: while(true) {
    // ... statement incl. break or continue
    }

    If you don't need continue, use do .. while(false) instead.
    Tor Iver Wilhelmsen, Nov 13, 2005
    #14
  15. On Sun, 13 Nov 2005 23:28:40 +0100, Tor Iver Wilhelmsen wrote:

    > Roedy Green <> writes:
    >
    >> outer:
    >> > {

    >
    > break/continue only work with loop labels, so you need to make a dummy
    > one around the block, e.g.
    >
    > outer: while(true) {
    > // ... statement incl. break or continue
    > }
    >
    > If you don't need continue, use do .. while(false) instead.


    Incorrect.

    class T {
    void method () {
    block : { int x = 0; break block; }
    }
    }

    works just fine

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Nov 14, 2005
    #15
  16. On Sun, 13 Nov 2005 14:38:18 +0000, Chris Uppal wrote:

    >
    > Interestingly, the C++ source for the native part of the AWT implementation
    > makes heavy use of goto in order to /improve/ the clarity, structure, and
    > maintanability of the code. Works too...


    I agree here. There are gotos and gotos. While generalizations are very
    hard, i find that as long as gotos always jump out of scopes, never into,
    and are "downstream", they hardly ever do serious harm, and can greatly
    untangle conditionals. :)

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Nov 14, 2005
    #16
  17. Mike42

    Roedy Green Guest

    On Sun, 13 Nov 2005 16:14:24 -0500, "Rhino"
    <> wrote, quoted or indirectly
    quoted someone who said :

    >
    >I *think* the anti-goto advice is primarily a reaction by structured
    >programmers to the preceding era of programming.


    I recall writing a C macro. One way it could be done with goto that
    required about 1/3 as much code. Yet the boss still wanted to go the
    long winded way since he knew that goto was bad.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 14, 2005
    #17
  18. On Sun, 13 Nov 2005 09:26:41 -0500, "Rhino"
    <> wrote:

    >
    >Luckily, Java doesn't have a 'goto' but, even if it did, you should avoid it
    >like the plague. Ditto for 'goto' in any other programming language.
    >


    Java uses break and continue, which take a parameter in the same way a goto
    statement requires. The only difference is that the label must be bound to
    a loop (allowing to break out to a specific loop, rather than using a
    hackish sentry value).
    Raymond Martineau, Nov 14, 2005
    #18
  19. On Sun, 13 Nov 2005 02:36:22 +0000, Roedy Green wrote:

    > On Sat, 12 Nov 2005 17:05:43 -0500, "Mike42"
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >>Is there a goto statement or something similar in java? If so, how do I
    >>use it?

    >
    > Break. It lets you leap out of a loop or loop nest.
    >
    > See http://mindprod.com/jgloss/jcheat.html


    Actually, break is much more powerful then that. break lets you terminate
    any instruction abruptly. You need not even be inside any block construct:

    label: break label; is a perfectly legal no-op.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Nov 14, 2005
    #19
  20. Chris Uppal wrote:
    >
    > Interestingly, the C++ source for the native part of the AWT implementation
    > makes heavy use of goto in order to /improve/ the clarity, structure, and
    > maintanability of the code. Works too...


    Does it? For colour management it does, but not so much elsewhere within
    AWT.

    Most uses of goto in the J2SE source are for error handling and resource
    freeing. Not particularly useful in Java.


    I don't think it's goto which are the issue. Although it isn't as clear
    as it might be. The problem is using control flow structures that do not
    mirror if, for, while, exceptions, etc.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Nov 14, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. youngsun park
    Replies:
    2
    Views:
    605
    David Pursley
    Nov 18, 2003
  2. Bo Peng
    Replies:
    31
    Views:
    780
    Ron Adam
    Jun 30, 2005
  3. MotoK
    Replies:
    59
    Views:
    1,772
    Keith Thompson
    Sep 15, 2006
  4. Peng Yu
    Replies:
    8
    Views:
    334
    Steven D'Aprano
    Nov 20, 2009
  5. Patrick Sabin
    Replies:
    1
    Views:
    306
    Paul Rudin
    Nov 20, 2009
Loading...

Share This Page