[SalesForce] Equivalent goto statement in Salesforce Apex

Is there an equivalent Goto Statement in Salesforce?
I need this to skip lot of lines of code from execution post a functional criteria is mismatched . Currently i observe, my logic is able to show a custom exception on the Page, but code will still continue to execute lot of lines -which i would like to skip in case of failure and move directly to the end line of the code.
In such situation, i used to use goto statement in other languages but unable to find any replacement in salesforce. Please advice.

Best Answer

Missing from David's answers are alternative solutions to complicated if statements, which is usually why people resort to such things. One really nice trick that kind of simulates goto is a do-while loop. Simply, you can have your code execute once (and only once), but leave a way to get out of the entire block quickly without throwing an exception.

do {
  ... code ...
  if(condition1) {
    break;
  }
  ... more code ...
  if(condition2) {
    break;
  }
  ... even more code ...
} while(false);

The false value at the end forces the loop to run only once, but you get the convenience of multiple exit points that are arguably easier to read than severely nested if statements; it really feels like a goto, but without the associated traps.

Of course, if you have nested loops, that trick won't help you, as you can't skip back out an arbitrary number of levels... or can you?

Boolean break1 = false, break2;
for(Integer x = 0; !break1 && x < 10; x++) {
  break2 = false;
  for(Integer y = 0; !break2 && y < 10; y++) {
    for(Integer z = 0; z < 10; z++) {
      if(condition1) {
        break; // goto next y
      }
      if(condition2) {
        break2 = true;
        break; // goto next x
      }
      if(condition3) {
        break1 = true;
        break; // Exit all loops
      }
    }
  }
}

Yes, it's slightly more code, but you can break out of any nested loop in a controlled manner with just a few simple Boolean variables. This makes the intent clear, unlike goto, which allows you to jump in from any arbitrary location.

And I can tell you, since I've started writing Apex Code, I can probably count the number of times I've used either technique on one paw, with a few digits left over. It's simply not necessary, because we have so many better tools at our disposal. Write more methods, and it becomes a matter of just writing "return" to break out of arbitrary groups of loops. Use classes to group bits of data and processing together.

Worst case scenario, just write a state machine and have it go from state to state. Technically, we use this "state machine" mechanism all the time. What do you think Visualforce controllers are? Exactly. Write a controller class, and call the methods on it as you need to, the class itself maintains state. In Java, it's very common to do this, but I think most Apex Code developers don't think of classes as much more than a place to store data and methods, rather than a complete machine by itself that can do incredibly complicated things.

Goto was from a time when compilers were simple and languages were simple, and it was really hard to get from point A to point B, so they needed a shortcut every once in a while. With a little bit more forethought, all code can be written in a way that never requires a goto.