continue ALLWAYS Illegal in switch in JS but break works fine

11,993

Solution 1

continue has absolutely nothing to do with switches, 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.

Share:
11,993
Owyn
Author by

Owyn

Updated on June 24, 2022

Comments

  • Owyn
    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
    Owyn over 10 years
    I expect it to go check next case statement below one with continue
  • Owyn
    Owyn over 10 years
    Yes, 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
    cliffroot over 10 years
    what about just not putting 'break' keyword?
  • Akhil Sekharan
    Akhil Sekharan over 10 years
    In that case, why don't you just put a break; in B.
  • Owyn
    Owyn over 10 years
    because I also need continue there, conditionally, before break as I wrote above.
  • Owyn
    Owyn over 10 years
    then it won't break and I also need it to break.
  • Akhil Sekharan
    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
    Owyn over 10 years
    it 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
    Akhil Sekharan over 10 years
    In JS also, you can use a continue, in switch, provided your switch statement is inside a loop
  • Owyn
    Owyn over 10 years
    If you copy my code into chrome console it would give a syntaxerror immediately saying you can't use it in switch.
  • Owyn
    Owyn over 10 years
    So there is no shortcut to "falling through" in switches?
  • Akhil Sekharan
    Akhil Sekharan over 10 years
    Its because there is no outer loop
  • Lightness Races in Orbit
    Lightness Races in Orbit over 10 years
  • Esailija
    Esailija over 10 years
    LOL I don't know C++ at all so I had to take the OP's word for it... lmao
  • Lightness Races in Orbit
    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
    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 to break" because that's a total contradiction
  • Owyn
    Owyn over 10 years
    both at two different conditions inside the case not at once
  • icl7126
    icl7126 about 9 years
    It should be noted that using continue in switch is OK as long as your switch is inside a loop. In that case, continue will actually break the switch.
  • Lightness Races in Orbit
    Lightness Races in Orbit about 9 years
    @klerik: It'll do more than break the switch; it'll skip over everything in the current loop iteration, and confuse your fellow programmers. In that instance you have a continue in a loop, so to say that it's "in" a switch is misleading. Anyway I wouldn't say it's "OK" because it is spaghetti code.
  • icl7126
    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) inside switch (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
    Lightness Races in Orbit about 9 years
    @klerik: No offence taken! I'm simply disagreeing!
  • Gerald LeRoy
    Gerald LeRoy almost 6 years
    It is possible to use continue in a switch statement - if - the swtich statement is inside a loop.