Why can't I use 'continue' inside a switch statement in Java?

南笙酒味 提交于 2019-11-30 05:32:19

问题


Why is it that the following code:

class swi  
{
    public static void main(String[] args)  
    {  
        int a=98;
        switch(a)
        {
            default:{ System.out.println("default");continue;}
            case 'b':{ System.out.println(a); continue;}
            case 'a':{ System.out.println(a);}
        }
        System.out.println("Switch Completed");
    }
}

Gives the error:

continue outside of loop


回答1:


Falling through is the standard behavior for a switch statement and so, consequently, using continue in a switch statement does not make sense. The continue statement is only used in for/while/do..while loops.

Based on my understanding of your intentions, you probably want to write:

System.out.println("default");
if ( (a == 'a') || (a == 'b') ){
    System.out.println(a);
}

I would also suggest that you place the default condition at the very end.

EDIT: It is not entirely true that continue statements cannot be used inside switch statements. A (ideally labeled) continue statement is entirely valid. For example:

public class Main {
public static void main(String[] args) {
    loop:
    for (int i=0; i<10; i++) {
        switch (i) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 9:
            continue loop;
        }

        System.out.println(i);
    }
}
}

This will produce the following output: 0 2 4 6 8




回答2:


The continue-Statement may be used in loops and not in switch. What you probably want is a break.




回答3:


Because you have a continue outside of a loop. continue is for jumping back to the beginning of a loop, but you don't have any loop in that code. What you want for breaking out of a switch case block is the keyword break (see below).

There's also no need to put every case block within braces (unless you want locally-scoped variables within them).

So somethinga bit like this would be more standard:

class swi22
{
    public static void main(String[] args)
    {
        int a=98;
        switch(a)
        {
            default:
                System.out.println("default");
                break;
            case 'b':
                System.out.println(a);
                break;
            case 'a':
                System.out.println(a);
                break;
        }
        System.out.println("Switch Completed");
    }
}

There's also a school of thought that says the default condition should always be at the end. This is not a requirement, just a fairly widely-used convention.




回答4:


continue inside switch??!!! only break can be kept inside switch.!

ur code should be,

class swi22  
{
     public static void main(String[] args)  
     {  
         int a=98;
         switch(a)
         {
             default:{ System.out.println("default");break;}
             case 'b':{ System.out.println(a); break;}
             case 'a':{ System.out.println(a);}
          }
          System.out.println("Switch Completed");
     }
}



回答5:


Shouldn't you use break instead of continue?




回答6:


You are using continue where you should be using break




回答7:


continue simply moves directly to the next iteration of the loop.

break is used to break out of loops and switches.

Use break; instead of continue;

Continue:

for(x = 0; x < 10; x++)
{
   if(x == 3)
     continue;
   else       
     DoIterativeWork();       
}

Switch:

switch(a)
{
 default:{ System.out.println("default"); break;}
 case 'b':{ System.out.println(a); break;}
 case 'a':{ System.out.println(a);}
}


来源:https://stackoverflow.com/questions/2521721/why-cant-i-use-continue-inside-a-switch-statement-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!