continue ALLWAYS Illegal in switch in JS but break works fine
continue
has absolutely 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 within a loop
If you wish to break out of the case, use break
; otherwise, allow the case to fall through:
switch ("B")
{
case "A":
break;
case "B":
case "C":
break;
default:
break;
}
If you're looking for a shortcut to jump to the next case then, no, you can't 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;
}
I believe you can emulate what you want by using 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 consider expressing what you want in some other way.
Even if you can pull this off, it would be a huge WTF. Just use if else if.
I think what you meant is:
switch ("B")
{
case "A":
break;
case "B":
case "C":
break;
default:
break;
}
There is no need for continue
. When B comes, it will move on to C.