Java

Java break and continue Statements

The continue statement is used in the body of a while, do and for loop. The break statement is used in these loops, as well as in the switch statement. Iteration can be considered the execution of a loop’s body, over and over. The break statement stops the iteration. The continue statement skips one execution (rest of the statements below) of the body. This article explains the employment of the continue and break statements in Java. The while-loop compound statement will be used. For the break statement, the switch compound statement will also be used.

A While-Loop

The following while-loop prints numbers from 1 to 5.

            int i = 0;
            while (i < 5) {
                ++i;
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1 2 3 4 5

The first statement is indirectly part of the while-loop. It is a starting state. The last statement is not part of the while-loop. The while-condition is “while (i < 5)”.

Article Content

The continue Statement
continue Statement in One While-Loop

In the following while-loop, the execution (rest of the statements below, continue) of the body, is skipped, when i equals 2:

            int i = 0;
            while (i < 5) {
                ++i;
                if (i == 2)
                    continue;
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1 3 4 5

The statement(s) below the condition, of i == 2, did not execute.

continue Statement in a Nested Loop

The continue statement in a nested loop affects only the nested loop. It does not send control to the outer loop. The following code illustrates this:

            char i = '@';
            while (i < 'E') {
                ++i;
                int j = 0;
                while (j < 5) {
                    ++j;
                    if (j == 2)
                        continue;
                    System.out.print(j); System.out.print(' ');
                }
                System.out.println();
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1 3 4 5
    A 1 3 4 5
    B 1 3 4 5
    C 1 3 4 5
    D 1 3 4 5
    E

continue Statement with Label Identifier, in One Loop

The continue command may have an argument, which is the identifier of a label. In Java, the label allows the recommencement of the loop. The effect is not noticeable for a single loop. The following code segment illustrates this:

            int i = 0;
            lbl :
            while (i < 5) {
                ++i;
                if (i == 2)
                    continue lbl;
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1 3 4 5

continue Statement with Label in a Nested Loop

If the label targets the outer loop, which has the inner loop, then there will be some effect. The following code illustrates this:

            char i = '@';
            lbl :
            while (i < 'E') {
                ++i;
                int j = 0;
                while (j < 5) {
                    ++j;
                    if (j == 2)
                        continue lbl;
                    System.out.print(j); System.out.print(' ');
                }
                System.out.println();
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1 1 1 1 1

The break Statement
break Statement in One While-Loop

In the following while-loop, the rest of the statements below the break statement, and the rest of the iterations, stop, when i equals 2:

            int i = 0;
            while (i < 5) {
                ++i;
                if (i == 2)
                    break;
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1

break Statement in a Nested Loop

The break statement in a nested loop affects both the nested loop, and the outer loop. It sends control to the outer loop. The following code illustrates this:

            char i = '@';
            while (i < 'E') {
                ++i;
                int j = 0;
                while (j < 5) {
                    ++j;
                    if (j == 2)
                        break;
                    System.out.print(j); System.out.print(' ');
                }
                System.out.println();
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1
    A 1
    B 1
    C 1
    D 1
    E

break Statement with Label Identifier, in One Loop

The break command may have an argument, which is the identifier of a label. In Java, the label allows the recommencement of the loop. The effect is not noticeable for a single loop. The following code segment illustrates this:

            int i = 0;
            lbl :
            while (i < 5) {
                ++i;
                if (i == 2)
                    break lbl;
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1

break Statement with Label in a Nested Loop

Consider the following code:

            char i = '@';
            lbl :
            while (i < 'E') {
                ++i;
                int j = 0;
                while (j < 5) {
                    ++j;
                    if (j == 2)
                        break lbl;
                    System.out.print(j); System.out.print(' ');
                }
                System.out.println();
                System.out.print(i); System.out.print(' ');
            }
            System.out.println();

The output is:

    1

The Switch Compound Statement and break

The switch compound statement transfers control to one of several shorter compound statements, depending on the value of an expression, which is the switch argument. The type of Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type. The shorter compound statements are begun by case labels. A shorter compound statement is a case block. In Java, after the case block has been identified, to stop the execution from continuing to the case blocks below it, each case block has to end with a “break;”. The following code illustrates this:

        int k = 2;
        switch (k) {
            case 1: System.out.print("one");
                    System.out.print("one");
                    System.out.print("one");
                    break;
            case 2: System.out.print("two");
                    System.out.print("two");
                    System.out.print("two");
                    break;
            case 3: System.out.print("three");
                    System.out.print("three");
                    System.out.print("three");
                    break;
        }

The output is:

    twotwotwo

Conclusion

The break statement stops the iteration. The continue statement skips one execution (rest of the statements below) of the body. If the programmer does not want execution in a switch statement to continue below a particular case block, a break statement has to end that case block.

Other aspects to take into consideration with the continue and break statements are as follows: continue Statement in One Loop; continue Statement in a Nested Loop; continue Statement with Label Identifier, in One Loop; continue Statement with Label in a Nested Loop; break Statement in One Loop; break Statement in a Nested Loop; break Statement with Label Identifier, in One Loop; break Statement with Label in a Nested Loop; and The Switch Compound Statement with breaks.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.