9
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
  • 663
  • 2
  • 8
  • 17
  • You need to explain what you want this code to do, because `continue` is not defined for `switch` (no matter how much you wish it to be), and we cannot divine your requirements from an example of a language feature that does not exist. – Lightness Races in Orbit Aug 06 '13 at 10:14
  • I have worked out — after much effort — that you wish to **jump** to the next case in the list (you keep calling it "break", but "break" means to leave the switch entirely). There is simply no syntax to do that, as I discuss in my answer. – Lightness Races in Orbit Aug 06 '13 at 10:23
  • This is unfortunate, since I have encountered an instance of a data structure that would benefit from this sort of behavior. `obj: { dims: [...], vars: [..., atts: [...], ...], atts: [...] }` , where I use a path to determine a part being referenced: dims.name, atts.name, vars.name.atts.attName, where often both atts are processed the same, because they have the same internal properties. i.e. `switch (_path[0]) { case 'dims': break; case 'vars': ...if (_path[2] === 'atts') continue; /* on to 'atts'*/ break; case 'atts': break; }` Alas, must simply resort to trusty if/elses. – GG2 Apr 01 '20 at 20:22

6 Answers6

13

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;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    LOL I don't know C++ at all so I had to take the OP's word for it... lmao – Esailija Aug 06 '13 at 10:14
  • 3
    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`. – icl7126 Apr 06 '15 at 09:10
  • @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. – Lightness Races in Orbit Apr 06 '15 at 12:52
  • @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. – icl7126 Apr 06 '15 at 14:12
  • 1
    @klerik: No offence taken! I'm simply disagreeing! – Lightness Races in Orbit Apr 06 '15 at 15:57
12

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.

Esailija
  • 138,174
  • 23
  • 272
  • 326
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.

Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
  • 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 – Owyn Aug 06 '13 at 09:48
  • In that case, why don't you just put a `break;` in `B`. – Akhil Sekharan Aug 06 '13 at 09:52
  • because I also need continue there, conditionally, before break as I wrote above. – Owyn Aug 06 '13 at 09:52
  • 1
    `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 – Akhil Sekharan Aug 06 '13 at 09:54
  • it have to do the continuing in switch if you ask this, and continue in C++ is meant to be used in switches btw – Owyn Aug 06 '13 at 09:57
  • In JS also, you can use a `continue`, in switch, provided your switch statement is inside a loop – Akhil Sekharan Aug 06 '13 at 10:00
  • If you copy my code into chrome console it would give a syntaxerror immediately saying you can't use it in switch. – Owyn Aug 06 '13 at 10:02
  • Its because there is no outer loop – Akhil Sekharan Aug 06 '13 at 10:05
  • 1
    @Owyn: No, [`continue` is _not_ meant to be used in switches in C++](http://ideone.com/w02zJE). Not at all. – Lightness Races in Orbit Aug 06 '13 at 10:10
1

Actually you can use it in switch inside loops, in this cases break/continue are a useful combo

take a look at this example

var array = [1, 2, "I am a string", function(){}, {}, 1,2,3]
for(let i in array){
    switch(typeof array[i]) {
    case "number":
        array[i] += " I was a number"
        break; // break from the switch but go on in the for-loop
               // ie. go to *put esclamation mark*
    case "function":
        array[i] = "function"
        // fall through
    default:
        array[i] = `this is a ${JSON.stringify(array[i])} object`
        break; // also here goes to *put esclamation mark*
    case "string":
        continue; // continue with the next iteration of the loop
                  // ie. do not put exclamation mark in this case
    }
    array[i] += "!" // *put esclamation mark*
}
/*
array will become
[ "1 I was a number!", "2 I was a number!", "I am a string", "this is a \"function\" object!", "this is a {} object!", "1 I was a number!", "2 I was a number!", "3 I was a number!" ]
*/
asdru
  • 1,147
  • 10
  • 19
0

A switch case without the break statement will "fall through" (as it's called) to the next. Omitting break will make the code behave like you say you expect as it is the default/implicit behavior of the switch statement.

The use of the continue statement is reserved for loops where what it does is actually slightly different from what you want; it will break the current iteration of the loop and continue to the next one, reevaluating the loop condition.

nordhagen
  • 799
  • 5
  • 18
  • So there is no shortcut to "falling through" in switches? – Owyn Aug 06 '13 at 10:03
  • @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 – Lightness Races in Orbit Aug 06 '13 at 10:21
-1

Continue may only refer to the loop. It can't be used inside the switch statement. What behavior do you expect from it, anyway?

cliffroot
  • 1,839
  • 17
  • 27