O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required