wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java and Unconditional Branching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java and Unconditional Branching" Watch "Java and Unconditional Branching" New topic
Author

Java and Unconditional Branching

akaii akaii
Greenhorn

Joined: Feb 15, 2005
Posts: 2
goto doesn't seem to exist for Java...can anyone suggest any uncomplicated alternatives? I'm writing code that needs jumping backwards... Looping seems to be a difficult option, since jumps overlap within each other, and aren't self-contained.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Dijkstra's famous Goto Considered Harmful paper was originally titled "unnecessary" but was spiced up by a publisher. I'd bet that if you can describe your logic in pseudo code or structured English we can help find a more elegant solution.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


goto doesn't seem to exist for Java

And thats a Very Good Thing. Acutally, it does exist in Java: goto is a reserved keyword, it just does nothing.

Caveat: Before you start writing procedural-style programming in an OO language, make sure you are absolutely sure it is the best way of doing something. From my experience (though this is by no means universal) with the exception of a few odd conditions,its almost always a bad idea: the only real excuse has been performance.

There are ways to "jump about" in Java. keywords like "break" and "continue" will interupt the normal flow of a loop or conditional to go to another point in the code. You might also want to look at labels - you can break to a named label again avoiding the normal exceution of a loop or conditional.

"Jump backwards" sounds like writing you code in a recursive way may also be a good alternative.

You might also consider describing the problem you are trying to solve here - perhaps someone can offer a better solution to procedural execution.

[ February 15, 2005: Message edited by: Paul Sturrock ]
[ February 15, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Paul Sturrock:
perhaps someone can offer a better solution to procedural execution.


Well, actually "jumping around in the code" isn't even procedural...

But agree, there *has* to be a better, more structured solution.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
usually, when i find myself writing code with a lot of "jumping around", i take it as a sign it's not adequately abstracted. most of the time i try to do more functional decomposition on it, perhaps rewrite parts or all of it in a more functional style; if nothing else, that helps me read it more easily, and i find it also helps refactoring. your mileage may vary, though.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by akaii akaii:
Looping seems to be a difficult option, since jumps overlap within each other, and aren't self-contained.
When you use break or continue in a loop, you can give it a label. This is necessary when you need to break/continue an outer loop rather than the inner loop. I think I've only ever done this once when I was learning Java, but IIRC the syntax is like this:Obviously you don't need the "inner" label here; it's just an example.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The first language I worked in didn't have subroutine definitions like functions or even COBOL paragraphs. Instead you could "perform" any range of line numbers anywhere in the program. One time you'd perform 101-105, another time 103-108. You never needed to copy-paste a line of code, you could just perform it in place. (Copy-paste is a bit involved with punch cards anyhow.) Anyhow, with that kind of spaghetti behind me I'm pretty darned happy with no GOTOs!
akaii akaii
Greenhorn

Joined: Feb 15, 2005
Posts: 2
What I'm essentially trying to do looks like this:

Label1:
/ code /

Label2:
/ code /
if (condition){
/ code /
goto Label1
}

Label3:
/ code /
if (condition){
/ code /
goto Label1
}

Label4:
/ code /
if (condition){
/ code /
goto Label3
}
else {
/ code /
goto Label3
}

I don't think I can convert these into loops, because it would end up looking something like this:

begin LoopA

begin LoopB

end LoopA

end LoopB

Believe me, I try to avoid using goto, I'm just under huge time constraints, is all.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
First, you never reference lable2 or label4, but I've left them in to help line it up. Second, your inner loop never terminates as both conditions at the end go back to Label3.

But let's see how close I can get. I've lettered (A-H) the "/ code /" lines and the conditions (a-c) from the top for clarity.As you can see, nothing tells it to bail out of the whole nested mess, so it's a nice infinite loop. Just set the boolean flags to true when you want to exit. Also, you can drop the two "continue Label3" statements since you are continuing the loop either way -- they have no effect.

All I can say is ... ugly! I bet you could find a better way to do that without so much nastiness. By the way, I didn't test that in any way. Good luck!
[ February 16, 2005: Message edited by: David Harkness ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Can you tell us *why* you need to jump that way? That is, explain us the requirements behind that code?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Well, actually "jumping around in the code" isn't even procedural...

Ah yes. Poor choice of words.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I revised the code & cpondition names in your example and tried to unravel it. See if it gives the same results for various conditions:

That was a real quicky. Lemme know if it fails.

Oh, and one more shortcut:

This never ends, but I don't think the original did either. Change while(true) to while(something that gets changed).

Whew, third edit: BTW, that assertion that "GOTO is unnecessary" was not just an opinion. It's one of those things that can be proven with deep enough math. Any possible logic can be expressed with sequential statements, alternation (if-then-else) and repetition (loops). Subroutines, recursion, all that stuff is nice, but optional. You could smash any algorithm into one giant method as I did with my two examples above. Truly ugly and evil, but possible.

[ February 16, 2005: Message edited by: Stan James ]
[ February 16, 2005: Message edited by: Stan James ]
 
jQuery in Action, 2nd edition
 
subject: Java and Unconditional Branching