Break and Continue on Switch

Asked

Viewed 1,903 times

3

What’s the difference of using the break and the continue within the switch?

1 answer

5


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

In a loop the continue goes to the end but stays within it unless the repeat condition sends out. Break` brings out the loop in any situation.

But you want to know switch which is a check flow control command and not repeat. The break in this building brings out the whole switch. The normal of each case is to perform what is inside if met the condition and go to the next case. If nothing is done he will try to analyze all case. It does not have an automatic short circuit. Only in most cases it is not what you want. It is common to enter one of the case you no longer want to check any other case, even because it is unlikely that any will meet the condition. The way to close is with the break. If not using occurs the call fallthrough automatic.

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

So it’s a misfortune that if you want to come out of a loop with a break and the code is inside a switch, the break will not leave the loop, will only exit the selector. Then you would have to do some extra control, or if the language allows, Java is one of them, use a break *label*. Has a reply in Kotlin on this, but serves more or less the same for Java.

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

In some cases a return may be the solution when it needs to terminate, after all well written methods are short and probably whether it is to get out of a switch and a tie, is also to get out of the method.

Has a question with an example (another). And there’s an example, albeit in C#, but it’s the same thing, for not using the break, it’s like I have a continue implicit. Therefore the continue is not used.

I said more about him in C. And how it works internally (Java is a little 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;
            }
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.