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

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

There is no goto in Java and for a very good reason.
You should not work the way you propose, you should instead call a different method from each condition instead of jumping to a fixed position in the same method.

The same goes for C as well. Though goto is available there its use should be (and is by most people) actively discouraged except under extreme conditions where the few microsecond performance difference between a direct jump and a function call make a critical difference.

For that reason goto is a reserved word in Java but its use will generate a compiler error.

if you want to get out of the if statement just use the break; command. It will get out of the nearest set of braces

This old thread from 2005 was revived by someone advertising their software library. That spam post has been deleted and I'm going to close this thread we don't get more people treating this as an open question.

@sirlink99: I'd encourage you to try Norm's suggestion above. It may turn out differently than you expect.

Edited 5 Years Ago by Ezzaral: n/a

This article has been dead for over six months. Start a new discussion instead.