Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

continue ALLWAYS Illegal in switch in JS but break works fine

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?

like image 590
Owyn Avatar asked Aug 06 '13 09:08

Owyn


1 Answers

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;
}
like image 152
Lightness Races in Orbit Avatar answered Oct 22 '22 00:10

Lightness Races in Orbit