Search code examples
javaandroidgoto

What's the equivalent for C#'s goto statement in Java?


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


Solution

  • 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.