GOTO equivalent in Java

Asked

Viewed 4,966 times

4

Well guys I’d like to know how to do a drop in java? or some way to get back to a certain point in the code, people say that it is dangerous to use this kind of command in java... why?

  • As far as I know, in the Java language there is no unconditional jump, but it is available in JVM bytecode, if you want to risk ;-)

  • Usually but not always, GOTO is a bad modeling. Djikstra (the same guy as the algorithm of the shortest distance between two vertices in a graph) even wrote an article: GOTO considered dangerous (http://homepages.cwi.nl/~Storm/Teaching/Reader/Dijkstra68.pdf)

1 answer

5


There is no direct equivalent to the GOTO concept in Java(1). There are some options that allow you to do some of the things similar to the classic GOTO. According to this reply on Soen, an option is you use the BREAK. See how it would be:

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;
    }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.