LiquorWuYan

Java Break 和 Continue 结束语句

  • break在任何循环语句中,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)

示例:

package com.shun.struct;

public class BreakDemo {
    public static void main(String[] args) {
        //breack强制退出
        int i = 0;
        while (i<100){
            i++;
            if (i==30){
                break;//强制退出,强制退出循环,但不影响循环语句之外的语句
            }
            System.out.println(i);//输出的结果是1~29
        }
        System.out.println("---------");//可以输出
        System.out.println("27");//可以输出
    }
}
  • continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,再重新执行下一次循环

示例:

package com.shun.struct;

public class ContinueDemo {
    public static void main(String[] args) {
        //Continue
        int i = 0;
        while (i<100){
            i++;
            if (i%10==0){
                System.out.println();//这个代表在输出中打一个回车键
                continue;
            }
            System.out.print(i);//输出的结果是没有10或10的倍数的所有1~100内的数
        }
        /*
        continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,再重新执行下一次循环
         */

    }
}

分类:

技术点:

相关文章:

  • 2021-09-15
  • 2022-12-23
  • 2021-08-04
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
猜你喜欢
  • 2021-05-07
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2022-01-16
  • 2021-11-09
相关资源
相似解决方案