2013年7月20日 星期六

Return and Break and Contiune


  • break
// Using break to exit a loop.
class BreakLoop {
 public static void main(String args[]) {
 for(int i=0; i<100; i++) {
if(i == 10) break; // terminate loop if i is 10
 System.out.println("i: " + i);
 }
System.out.println("Loop complete.");
}
}

  • contiune
// Demonstrate continue.
class Continue {
 public static void main(String args[]) {
 for(int i=0; i<10; i++) {
 System.out.print(i + " ");
 if (i%2 == 0)
   continue;
System.out.println("");
}
}
}

  • return
// Demonstrate return.
class Return {
public static void main(String args[]) {
 boolean t = true;
System.out.println("Before the return.");
 if(t) return; // return to caller
 System.out.println("This won't execute.");
}
}

沒有留言:

張貼留言