Continue ALLWAYS Illegal transition to JS, but the break works fine

switch ("B") { case "A": break; case "B": continue; case "C": break; default: break; } 

simple correct code in C ++, but when it is made in javascript in stable chrome, it just throws an "Illegal continue statement" error, it looks like the continue statement is simply forbidden in the javascript switch ... I heard about the return, but it just returns and does not continue ... So is there a way to continue the transition to js?

+4
source share
5 answers

continue has nothing to do with switch es, not in Javascript and not in C ++ :

 int main() { int x = 5, y = 0; switch (x) { case 1: continue; case 2: break; default: y = 4; } } 

error: continue statement not in a loop

If you want to exit the enclosure, use break ; otherwise let the case fail:

 switch ("B") { case "A": break; case "B": case "C": break; default: break; } 

If you are looking for a shortcut to move on to the next case, no, you cannot do this.

 switch ("B") { case "A": break; case "B": if (something) { continue; // nope, sorry, can't do this; use an else } // lots of code case "C": break; default: break; } 
+9
source

I believe that you can emulate what you want with a labeled infinite loop:

 var a = "B"; loop: while( true ) { switch (a) { case "A": break loop; case "B": a = "C"; continue loop; case "C": break loop; default: break loop; } } 

Otherwise, you should think about what you want in some other way.

Even if you can take it off, it will be a huge WTF. Just use if else if.

+7
source

I think you meant:

 switch ("B") { case "A": break; case "B": case "C": break; default: break; } 

No need to continue . When B appears, it moves to C.

+3
source

Continuation can only mean a cycle. It cannot be used inside a switch statement. What behavior do you expect from this?

0
source

The case of a switch without a break statement will fail (as it is called) to the next. Omitting break will cause the code to behave as you say, expect, as this is the behavior / implicit behavior of the switch statement.

Using the continue statement is reserved for loops, where what it does is actually slightly different from what you want; it will break the current loop iteration and move on to the next, overestimating the loop condition.

0
source

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


All Articles