Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Is there a goto statement (or something similar)?

Thread view: 
Mike42 - 12 Nov 2005 22:05 GMT
Is there a goto statement or something similar in java?  If so, how do I
use it?
Stefan Ram - 12 Nov 2005 22:11 GMT
>Is there a goto statement or something similar in java?

 The statements "switch" or "throw" do something similar.

>If so, how do I use it?

 See

http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html
Stefan Ram - 12 Nov 2005 23:03 GMT
>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."
Luc The Perverse - 12 Nov 2005 23:26 GMT
>>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

:)
Roedy Green - 13 Nov 2005 02:36 GMT
On Sat, 12 Nov 2005 17:05:43 -0500, "Mike42"
<michael_adamus@nospam.yahoo.com> 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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Stefan Schulz - 14 Nov 2005 08:24 GMT
> On Sat, 12 Nov 2005 17:05:43 -0500, "Mike42"
> <michael_adamus@nospam.yahoo.com> wrote, quoted or indirectly quoted
[quoted text clipped - 6 lines]
>
> 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.

Signature

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"
         

John C. Bollinger - 13 Nov 2005 04:31 GMT
> 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.

Signature

John Bollinger
jobollin@indiana.edu

Roedy Green - 13 Nov 2005 06:29 GMT
On Sat, 12 Nov 2005 17:05:43 -0500, "Mike42"
<michael_adamus@nospam.yahoo.com> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Rhino - 13 Nov 2005 14:26 GMT
> 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
Chris Uppal - 13 Nov 2005 14:38 GMT
> 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
Roedy Green - 13 Nov 2005 16:49 GMT
On Sun, 13 Nov 2005 14:38:18 -0000, "Chris Uppal"
<chris.uppal@metagnostic.REMOVE-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?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Rhino - 13 Nov 2005 21:14 GMT
> > At the risk of sounding like an old geezer, every programming course I
> > have taken in over 20 years of professional programming has strongly
[quoted text clipped - 10 lines]
> 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
Roedy Green - 14 Nov 2005 01:33 GMT
On Sun, 13 Nov 2005 16:14:24 -0500, "Rhino"
<no.offline.contact.please@nospam.com> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Chris Uppal - 14 Nov 2005 12:20 GMT
> 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.

You are the first anti-goto campaigner (please excuse the exgageration) I have
ever heard admit that there might be /any/ circumstances where a goto could
improve the code.  You are indeed "an open-minded guy" ;-)

   -- chris
Stefan Schulz - 14 Nov 2005 00:47 GMT
> 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. :)

Signature

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"
         

Thomas Hawtin - 14 Nov 2005 09:53 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chris Uppal - 14 Nov 2005 12:22 GMT
> Does it? For colour management it does, but not so much elsewhere within
> AWT.

It seems to be mainly for cleaning up JNI references in code that is not
(ultimately) invoked from Java.

   -- chris
Raymond Martineau - 14 Nov 2005 04:20 GMT
>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).
Tony O'Bryan - 13 Nov 2005 14:55 GMT
> 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
   {
   }
=======================
Roedy Green - 13 Nov 2005 16:51 GMT
On Sun, 13 Nov 2005 14:55:22 GMT, Tony O'Bryan
<storm_reaver@yahoo.com> wrote, quoted or indirectly quoted someone
who said :

>try
>    {
[quoted text clipped - 12 lines]
>    {
>    }

you can also do that with:

outer:
>    {
>    if (...)
[quoted text clipped - 3 lines]
>            if (...)
>                {
                 break outer;
>                }
>            }
>        }
>    }
// continues here
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Tor Iver Wilhelmsen - 13 Nov 2005 22:06 GMT
> 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.
Stefan Schulz - 14 Nov 2005 00:45 GMT
>> outer:
>> >    {
[quoted text clipped - 7 lines]
>
> 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

Signature

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"
         



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.