Java:break和continue关键字的作用
2021-01-16 13:11
                         标签:continue   case   do while   sys   com   inf   pre   switch   语句    1. break:直接跳出当前循环体(while、for、do while)或程序块(switch)。其中switch case执行时,一定会先进行匹配,匹配成功返回当前case的值,再根据是否有break,判断是否继续输出,或是跳出判断。 2. continue:不再执行循环体中continue语句之后的代码,直接进行下一次循环。 
 运行结果: 可以看到测试 continue时,当 i==3,直接跳过了continue之后的输出语句,进入下一次循环。 在break测试中,当 i==2,直接跳出了 for 循环,不再执行之后的循环 Java:break和continue关键字的作用 标签:continue   case   do while   sys   com   inf   pre   switch   语句    原文地址:https://www.cnblogs.com/KevinFeng/p/12925651.html二者的作用和区别
public class Loop {
    public static void main(String[] args) {
        System.out.println("-------continue测试----------");
        for (int i = 0; i ) {
            if (i == 2) {
                System.out.println("跳过下面输出语句,返回for循环");
                continue;
            }
            System.out.println(i);
        }
        System.out.println("----------break测试----------");
        for (int i = 0; i ) {
            if (i == 2) {
                System.out.println("跳过下面输出语句,返回for循环");
                break;
            }
            System.out.println(i);
        }
    }
}
