JAVA- continue与break与return关键字

 

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...");
}

 

 

 

给TA打赏
共{{data.count}}人
人已打赏
Java博客

JAVA-三大语句(选择语句、条件语句、循环语句)

2019-8-17 17:11:12

Java博客

JAVA- 面向对象

2019-8-17 17:14:35

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索