1

Is there a way I could adopt the C#'s goto statement in Java?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mr.Noob
  • 1,005
  • 3
  • 24
  • 58
  • 12
    Please 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
  • 1
    Its 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 Answers7

4

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.

Chirag
  • 56,621
  • 29
  • 151
  • 198
3

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.

assylias
  • 321,522
  • 82
  • 660
  • 783
2

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

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

Labelled statements with break and continue.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
1

Read about using label statements in Java.

question

flow control

Community
  • 1
  • 1
Yegoshin Maxim
  • 872
  • 2
  • 21
  • 54
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tozka
  • 3,211
  • 19
  • 23
-3

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

Community
  • 1
  • 1
user1581900
  • 3,680
  • 4
  • 18
  • 21
  • 3
    When 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