我是靠谱客的博主 傲娇网络,这篇文章主要介绍在switch case里使用continue在switch case里使用continue,现在分享给大家,希望可以做个参考。

在switch case里使用continue

continue只能在循环体里使用

while,do while,for

continue不能只在switch case里使用

顾名思义,continue只能在循环体内使用,若想在switch里使用continue,则需要switch case在循环体内

while(true) {
    switch(i){
        case 1:
            continue;
        case 2:
            break;
    }
}

若不在循环体内使用continue会报错

switch(i) {
    case 1:
        continue;//报错
}

continue与break不能同时使用

switch(i) {
    case 1:
        continue;
        break;//报错
    case 2:
        break;
        continue;//报错
}

continue的作用

跳过剩下的语句直接执行下一次循环

for(int i=0; i<5; i++) {
    switch (i) {
    case 0:
        System.out.println(i);
        break;
    case 1:
        System.out.println(i);

        break;
    case 2:
        System.out.println(i);
        continue;

    case 3:
        System.out.println(i);

        break;
    case 4:
        System.out.println(i);

        break;
    }
    System.out.println("=====================");
}

输出

0
=====================
1
=====================
2
3
=====================
4
=====================

最后

以上就是傲娇网络最近收集整理的关于在switch case里使用continue在switch case里使用continue的全部内容,更多相关在switch内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(132)

评论列表共有 0 条评论

立即
投稿
返回
顶部