11、break、continue、goto

在这里插入图片描述

continue

package com.wzt.www.struct;

/**
 * @author WZT
 * @create 2021-03-25 21:53
 */
public class ContinueDemo {
    
    
    public static void main(String[] args) {
    
    
        int i = 0;
        while (i<100){
    
    
            i++;
            if (i%10==0){
    
    
                System.out.println();
                continue;
            }
            System.out.print(i+" ");
        }
    }
}

输出

1 2 3 4 5 6 7 8 9 
11 12 13 14 15 16 17 18 19 
21 22 23 24 25 26 27 28 29 
31 32 33 34 35 36 37 38 39 
41 42 43 44 45 46 47 48 49 
51 52 53 54 55 56 57 58 59 
61 62 63 64 65 66 67 68 69 
71 72 73 74 75 76 77 78 79 
81 82 83 84 85 86 87 88 89 
91 92 93 94 95 96 97 98 99 

Process finished with exit code 0

  1. break在任何循环语句的主体部分,均可用break控制循环的流程。
  2. break用于强行退出循环,不执行循环中的剩余的语句(break语句也在switch语句中使用)。
  3. continue语句用在循环语句体中,用于终止某次循环过程,就是跳过了循环体中尚未执行的语句,接着进行下一次是否执行循环的判断。

goto及Label

package com.wzt.www.struct;

/**
 * @author WZT
 * @create 2021-03-25 22:04
 */
public class LabelDemo {
    
    
    public static void main(String[] args) {
    
    
        //打印101-150之间的所有质数
        //质数是指大于1的自然数中,除了1和它本身以外不再有其他因数的自然数

        int count = 0;
        //不建议使用,重要的是质数
        outer:for (int i = 100; i < 150; i++) {
    
    
            for (int j = 2; j < i/2; j++) {
    
    
                if (i % j == 0){
    
    
                    continue outer;
                }

            }
            System.out.print(i+" ");

        }
    }
}

输出

101 103 107 109 113 127 131 137 139 149 
Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_45809838/article/details/115220197