• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java and Unconditional Branching

 
akaii akaii
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
M Beck
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us *why* you need to jump that way? That is, explain us the requirements behind that code?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Ah yes. Poor choice of words.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic