Java for循环的label和增强for语句

for循环的label

其实所谓的label就是一个任意跳转的指令

public class Example {
    public static void main(String[] args){
        label:for(int a=0;a<5;a++){
            for(int b=0;b<5;b++){
                System.out.println(a+b);
                if(a+b>=7){
                    break label ;
                }
            }
        }
    }
}

输出:

0
1
2
3
4
1
2
3
4
5
2
3
4
5
6
3
4
5
6
7

增强for语句

public class Example {
    public static void main(String[] args){
        int[] number = {1,2,3,4,5,6,7,8,9,10};
        int sum = 0;
        for(int i:number){
            sum += i;
        }
        System.out.println(sum);
    }
}

输出:

55

其实和python里的for i in ...很像

猜你喜欢

转载自blog.csdn.net/weixin_34390105/article/details/90863458
今日推荐