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 want to break out of an inner loop (see section 11.6.2).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.44.182