continue与break关键字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
for ( int i= 0 ;i< 10 ;i++){ if (i% 2 == 0 ){ continue ; //跳过当前循环执行下一次循环 } System.out.println(i); } for ( int i= 0 ;i< 10 ;i++){ if (i== 3 ){ break ;} //跳出当前循环 System.out.println(i); } 跳出指定的 for 循环体,和 goto 很像 K: for ( int i= 0 ;i< 3 ;i++){ //给这个for循环体取一个名字为K for ( int j= 0 ;j< 3 ;j++){ if (j== 1 ){ break K;} //如果你不指跳出的for循环,那么就是跳出本地for循环,这里指定K,则调试for循环名称为K的循环体 System.out.println(j+ "-----" ); } System.out.println(i); } System.out.println( "hello world" ); |
return关键字
1
2
3
4
5
6
7
8
9
10
11
12
|
return 关键字: public static void main(String[] args) { for ( int i= 0 ;i< 10 ;i++){ System.out.println( "hello world" ); System.out.println( 1 + 1 ); if (i== 3 ){ return ; //主线程返回,此时main方法弹栈,main方法弹栈,虚拟机退出 } System.out.println( "hello" ); } System.out.println( "end..." ); } |