GoTo

F

freesoft_2000

Hi everyone,

I need to use something in java that is similar to the C
language goto statement.

This is what i have in a function

Code:
public int ret()
{

if
{
//something
//something

//This is the part where i need to jump to the end of the
//function and not exit the function
}

//This is the part i need to jump to

//some more code
//some more code
}

Is there a way in which the above problem can be got around. Usually in
the C language i would use the goto statement. I can't use a labeled break
here as there is no loop in my program.

I hope someone can help me this problem

Thank You

Yours Sincerely

Richard West
 
B

Bjorn Abelli

...
I need to use something in java that is similar to the C
language goto statement.

It's always possible to structure the code so you don't *need* a goto
statement.
public int ret()
{

if
{
//something
//something

//This is the part where i need to jump to the
//end of the function and not exit the function

There is some logic missing in your example. If you *just* want to goto the
designated place, the rest of the statements inside the if are redundant as
they're *never* will be executed. I believe you really mean that it should
"goto" only at specific conditions?

In that case, why not simply use that condition to "skip" the rest of the
statements?

if ([condition for *not* jumping])
{
//The rest of the statements in the if
}
}

//This is the part i need to jump to

//some more code
//some more code
}

There are of course more possible alternatives, depending on what the actual
problem/code is about.
Is there a way in which the above problem can be
got around. Usually in the C language i would use
the goto statement.

"goto" is of many believed to be bad practice, even if the language supports
it, as such language-specific constructs makes code less portable to other
languages.

// Bjorn A
 
A

anonymous

freesoft_2000 said:
Hi everyone,

I need to use something in java that is similar to the C
language goto statement.

This is what i have in a function

Code:
public int ret()
{

if
{
//something
//something

//This is the part where i need to jump to the end of the
//function and not exit the function
}

//This is the part i need to jump to

//some more code
//some more code
}

Is there a way in which the above problem can be got around. Usually in
the C language i would use the goto statement. I can't use a labeled break
here as there is no loop in my program.

I hope someone can help me this problem

Thank You

Yours Sincerely

Richard West
Are you absolutely CERTAIN your logic cannot do without a goto?
I mean, with exceptions, for, while, if, switch, nested classes on
should think the toolbox isn't exactly lacking a goto instruction. Even
in every other language I have seen (apart from Basic), the textbooks
strongly advise NOT to use goto.
 
J

Juha Laiho

freesoft_2000 said:
I need to use something in java that is similar to the C
language goto statement.

This is what i have in a function

Code:
public int ret()
{

if
{
//something
//something

//This is the part where i need to jump to the end of the
//function and not exit the function[/QUOTE]

Ok; does this happen because something failed in such way that you cannot
continue the processing?
[QUOTE]
}

//This is the part i need to jump to
[/QUOTE]

And is this some kind of cleanup you need to do regardless of the failure
above?
[QUOTE]
//some more code
//some more code
}

Is there a way in which the above problem can be got around. Usually in
the C language i would use the goto statement. I can't use a labeled break
here as there is no loop in my program.

If my guesses were correct, then the natural way to solve this would be
a try/catch/finally -construct, where the cleanup code you always need to
perform goes into the finally block. When things didn't go as you expected,
you throw an exception, then handle that exception and continue with the
finally block.

The other solution would be to write the desired code flow with if/else -
constructs. As was said in another message, goto is never required.
 
D

Dimitri Maziuk

anonymous sez:
.... Even
in every other language I have seen (apart from Basic), the textbooks
strongly advise NOT to use goto.

101-level textbooks usually fail to mention exceptions.
State machines and named loops (in languages that don't
have them, Java does have break label) are 2 that immediately
come to mind.

To OP: you could fake it with labelled break, just add a
single-iteration loop:

for( int i = 0; i < 1; i++ ) {
...
// goto condition
break foo;
}
foo:
...

It's not pretty but it'll do what you want. And if the
compiler was really smart, it'd optimize away the loop,
but I don't think javac can do loop unrolling. Anyway,
you can probably live with one extra int and 4 extra
instructions.

Dima
 

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

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top