Is there a goto statement (or something similar)?

S

Stefan Ram

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."
 
L

Luc The Perverse

Stefan Ram said:
"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.
 
J

John C. Bollinger

Mike42 said:
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.
 
R

Roedy Green

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.
 
R

Rhino

Mike42 said:
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
 
C

Chris Uppal

Rhino said:
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
 
T

Tony O'Bryan

Mike42 said:
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
{
}
=======================
 
R

Roedy Green

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?
 
R

Rhino

Chris Uppal said:
Clearly we have not met ;-)



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
 
T

Tor Iver Wilhelmsen

Roedy Green said:

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.
 
S

Stefan Schulz

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
 
S

Stefan Schulz

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. :)
 
R

Roedy Green

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.
 
R

Raymond Martineau

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).
 
T

Thomas Hawtin

Chris 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...

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top