11.5 Flow control with the break and continue keywords


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



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net