continue ALLWAYS Illegal in switch in JS but break works fine
Solution 1
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;
}
Solution 2
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.
Solution 3
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.
Owyn
Updated on June 24, 2022Comments
-
Owyn almost 2 years
switch ("B") { case "A": break; case "B": continue; case "C": break; default: break; }
simple correct code in C++, but when made in javascript in stable chrome it just throws an error "Illegal continue statement", looks like continue statement is just disallowed in switch in javascript... Heard about return but it just returns and doesnt continue... So is there a way to continue switch in js?
-
Owyn over 10 yearsI expect it to go check next case statement below one with continue
-
Owyn over 10 yearsYes, I meant this and I know about this but I need this emptyiness as continue statement because I need to break; in all conditions in case "B" and only in one condition to continue, continuing as emptyness would require tons of garbage code to check it all before breaking and not guarantee that there won't be a case with unintentional continuing and not breaking
-
cliffroot over 10 yearswhat about just not putting 'break' keyword?
-
Akhil Sekharan over 10 yearsIn that case, why don't you just put a
break;
inB
. -
Owyn over 10 yearsbecause I also need continue there, conditionally, before break as I wrote above.
-
Owyn over 10 yearsthen it won't break and I also need it to break.
-
Akhil Sekharan over 10 years
continue
is meant to be in loops in order to skip execution of a particular item in a list. What does it have to do with a switch -
Owyn over 10 yearsit have to do the continuing in switch if you ask this, and continue in C++ is meant to be used in switches btw
-
Akhil Sekharan over 10 yearsIn JS also, you can use a
continue
, in switch, provided your switch statement is inside a loop -
Owyn over 10 yearsIf you copy my code into chrome console it would give a syntaxerror immediately saying you can't use it in switch.
-
Owyn over 10 yearsSo there is no shortcut to "falling through" in switches?
-
Akhil Sekharan over 10 yearsIts because there is no outer loop
-
Lightness Races in Orbit over 10 years@Owyn: No,
continue
is not meant to be used in switches in C++. Not at all. -
Esailija over 10 yearsLOL I don't know C++ at all so I had to take the OP's word for it... lmao
-
Lightness Races in Orbit over 10 years@Owyn: What the hell are you talking about? You either want it to break, or you want it to fall through to the next case. How could you possibly do both?
-
Lightness Races in Orbit over 10 years@Owyn: Falling through is the default behaviour — you do it by not writing
break
. No, you can't "jump" to the next case. And don't give me this nonsense "I also need it tobreak
" because that's a total contradiction -
Owyn over 10 yearsboth at two different conditions inside the case not at once
-
icl7126 about 9 yearsIt should be noted that using
continue
inswitch
is OK as long as yourswitch
is inside a loop. In that case,continue
will actuallybreak
theswitch
. -
Lightness Races in Orbit about 9 years@klerik: It'll do more than
break
theswitch
; it'll skip over everything in the current loop iteration, and confuse your fellow programmers. In that instance you have acontinue
in a loop, so to say that it's "in" aswitch
is misleading. Anyway I wouldn't say it's "OK" because it is spaghetti code. -
icl7126 about 9 years@LightningRacisinObrit I din't meant to offend you, I just wanted to improve your answer. I was trying to find out whether I can use
continue
(for my loop) insideswitch
(since 'similiar'break
will not work). And there is no spaghetti in this, It's just another use case. I've never used single 'goTo' stuff in my life. -
Lightness Races in Orbit about 9 years@klerik: No offence taken! I'm simply disagreeing!
-
Gerald LeRoy almost 6 yearsIt is possible to use continue in a switch statement - if - the swtich statement is inside a loop.