Break and Continue on Switch

1

What's the difference between using break and continue within switch ?

    
asked by anonymous 30.07.2018 / 02:22

1 answer

3
The switch itself does not accept a continue , but it accepts break , which can be a little confusing because the while , do ... while , for , for : both flow control commands that determine a deviation to the end of the loop.

In a loop the continue goes to the end but remains inside it unless the repeat condition causes it to exit. The'break'does leave the loop in any situation.

But you want to know of switch that is a control of selection flow control and not repetition. The break in this construct exits all switch . The normal of each case is to execute what is inside if it met the condition and go to the next case . If nothing is done it will attempt to parse all case . It does not have an automatic short-circuit. But in most cases it's not what you want. Often, if you enter one of the case you no longer want to check any other case , even though some is unlikely to meet the condition. The way to quit is break . If you do not use automatic fallthrough is called.

This is considered an error by many, but this is how languages have chosen to work. It would be better if the break were automatic and fallthrough would be explicitly explicit.

So it's a misfortune that if you want to exit a loop with a break and the code is within switch , the break will not exit loop , only exit the selector. There you have to do some extra control, or if the language allows, Java is one of them, use break *label* . You have a answer in Kotlin about it , but it is more or less the same for Java.

An alternative, not always good, is to use a if in sequence, so the break has the normal loop semantics, if it is necessary to use it for that.

In some cases a return may be the solution when you need to close, after all well written methods are short and probably if it is to exit a switch and a loop, it is also to exit the method.

You have a ask with an example ( other ). And it has an example, although in C #, but it is the same thing, for not using break , it is as if it had an implicit continue . This is why continue is not used.

I've talked about about it in C . And how it works internally (Java is a bit different, but not much).

See example:

class Program {
    public static void main (String[] args) {
        for (int x = 0; x < 10; x++) {
            if (x == 6) continue;
            if (x == 8) break;
            switch (x) {
            case 0:
                System.out.println("zero");
                continue;
            case 1:
                System.out.println("um");
                continue;
            case 2:
                System.out.println("dois");
                break;
            default:
                System.out.println(x);
                break;
            }
        }
    }
}

See running on ideone . And no Coding Ground . Also I placed in GitHub for future reference .

    
30.07.2018 / 02:31