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

Why is this 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 an error:

continue outside the loop

+4
source share
7 answers

Falling is the standard behavior for a switch statement, and therefore, therefore, using continue in a switch statement does not make sense. The continue statement is only used for 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 also suggest that you put the default condition at the very end.

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

 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 lead to the following output: 0 2 4 6 8

+14
source

continue -Statement can be used in a loop rather than a switch. You probably need a break .

+7
source

Because you have continue outside the loop. continue intended to go to the beginning of the loop, but you have no loop in this code. What you want to exit the case switch block is the break keyword (see below).

There is also no need to put each case block in curly braces (unless you want local variables in them).

So something 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 is also a school of thought, which states that the default condition should always be at the end. This is not a requirement, just a fairly widely used convention.

+6
source

continue inside the switch ?? !!! only break can be stored inside the 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"); } } 
+3
source

Shouldn't use break instead of continue ?

0
source

You use continue , where you should use break

0
source

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

break used to exit loops and switches.

Use break; instead of continue;

Proceed

 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);} } 
0
source

Source: https://habr.com/ru/post/1305198/


All Articles