Is there a way I could adopt the C#'s goto statement in Java?
-
12Please give an example of what you want to achieve. Even in C#, it's almost always better to avoid `goto` statements. – Jon Skeet Oct 08 '12 at 13:07
-
You need to be more clear what you want to do.. There are many who know Java but not C#.. – Rohit Jain Oct 08 '12 at 13:08
-
I also agree with @JonSkeet wt is your issue? – Hardik Joshi Oct 08 '12 at 13:08
-
Have a look at this: http://stackoverflow.com/questions/2430782/alternative-to-goto-statement-in-java – dngfng Oct 08 '12 at 13:09
-
i want to implement a TCP connection so incase there is an exception occured or disconnection i need to catch the exception and then re-establish a connection. – Mr.Noob Oct 08 '12 at 13:18
-
1Its so simple then put your connection code in one function and call that function when you got exception !! – Chirag Oct 08 '12 at 13:19
-
possible duplicate of [Is there a goto statement in java?](http://stackoverflow.com/questions/2545103/is-there-a-goto-statement-in-java) – Sam Oct 08 '12 at 17:00
7 Answers
There is no direct equivalent to the goto concept in Java. You can use break and continue .
Look at here for labelled statements with break and continue.

- 56,621
- 29
- 151
- 198
-
2Many random downvotes on java answers today... I just got two on this question too... – Denys Séguret Oct 08 '12 at 13:12
-
-
@user1581900 Its very silly reason. You can not dowvote any user answer to choose your answer as accepted . – Chirag Oct 08 '12 at 13:19
-
3@user1581900 Please use downvoting only for answers that are wrong or do not answer the question - not because you want to "win". – Jesper Oct 08 '12 at 13:19
-
@user1581900 lol, thats nuts. Probably better to just write the best answer quickest and play fair. Otherwise you'll soon lose friends. – NimChimpsky Oct 08 '12 at 13:20
-
user1581900 can't be the only one downvoting or he has greater powers than me. I suppose his comment was a joke or badly phrased. – Denys Séguret Oct 08 '12 at 13:21
-
Successful troll is a happy troll. I already feel like I won and I'm much better now. Thank you. I needed it today. You can always downvote my answer - try it - it feels MUCH better then upvoting. – user1581900 Oct 08 '12 at 13:22
-
There is no goto statement in Java:
Unlike C and C++, the Java programming language has no goto statement.
The closest you have are continue
and break
statements, which can be used in combination with labels, to exit from a loop.

- 321,522
- 82
- 660
- 783
There is no exact equivalent but Java took care of the most legitimate use case, that is when you want to go to some label from inside a loop. In this case you may use break :
outerLoop:
for (int i = 0; i < n; i++) {
t = A[[i]][j];
for (int z = 0; z < l; z++) {
if (arrays[z].contains(t)) {
continue outerLoop;
}
}
}
See also the examples in Oracle's tutorial

- 372,613
- 87
- 782
- 758
There are named blocks, and you can have a labeled break
statement:
loop:
for (int i = 0; i < 10; ++i)
break loop;
Or
label: {
if(something)
break label;
}
More information can be found in Branching Statements. But other than that, you don't have a real goto statement.

- 30,738
- 21
- 105
- 131

- 3,211
- 19
- 23
is there a way I could adopt the C#'s goto statement in JAVA?
The goto statement in java is a reserved keyword. However it is not implemented in any way. Probably there were plans to include it in the java engine.
To answer your question. You can used labeled break, which works very similarly to goto:
class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts = {
{ 32, 87, 3, 589 },
{ 12, 1076, 2000, 8 },
{ 622, 127, 77, 955 }
};
int searchfor = 12;
int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor +
" at " + i + ", " + j);
} else {
System.out.println(searchfor +
" not in the array");
}
}
}
Similar question was aswered here

- 1
- 1

- 3,680
- 4
- 18
- 21
-
3When you take an example from another answer or an official Oracle tutorial, it's better to include the [link](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html). – Denys Séguret Oct 08 '12 at 13:17