11.5. Flow control with the break and continue keywords

In Java, you cannot goto some statement, but you can label certain loops with a valid identifier and break or continue to that loop. An example of using the break keyword to terminate a loop prematurely is shown in the Java program below.

 1:  // Test.java
 2:  public class Test{
 3:    public static void main(String []args){
 4:
 5:      outerLoop: // loop label
 6:      while(true){
 7:        for (int i=0; i<10; i++){
 8:          System.out.println(i);
 9:          if (i==3)
10:            break outerLoop;
11:        }
12:      }
13:      System.out.println("end");
14:    }
15:  }

In C#, both break and continue cannot be used with a label. You cannot break <expression> or continue <expression> as you can in Java.

But you can still do that – use C#'s goto if you ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.