break,contine都是使用在循環體中的語句,都有終止執行的作用,具體不同看下面詳解。循環語句看這裡:
break;
例子:
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
break;
}
System.out.println(x);
}
}
}
這將產生以下結果:continue;
例子:
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
if( x == 30 ) {
continue;
}
System.out.println(x);
}
}
}
這將產生以下結果:
10