Difference between Break and Continue statement in java

difference between break and continue statement

Difference between Break and Continue statement in java

In java 53 reserved words which are known as Keyword and Break and Continue are also keyword in java. Both are use in control structure(loop or switch case). but still there is some difference between break and continue statement. Sometimes we confuse between break and Continue. That’s why it’s important to know about it. So in this blog i’m trying to give some clarity about break and continue statement in java.

Break Statement:

break is a keyword which terminates the execution of current loop and goes out from that loop body. break statement is used in loop as well as in switch case.

Continue Statement:

continue is also reserved word in Java which skip the current iteration execution when it occurs and let program flow to the next iteration. In while or do…while loop program control goes to the Boolean expression (condition).

 Also Read: Different Java Data Type – Primitive and Non-Primitive Data type

Also Read: Different types of Variables in Java – Based on it’s Value Representation and it’s Declaration Position

 

Break and Continue Statement Program :

class Continue {

    int array[] = {0,1, 2, 3, 4, 5, 6};

    void disp() {
        System.out.println("Through Continue:");
        for (int i = 0; i < 6; i++) {

            if (i == 1) {
                continue;
            } else {
                System.out.println(array[i]);
            }
        }
    }
}

class Break {

    int array[] = {0,1, 2, 3, 4, 5, 6};

    void disp() {
        System.out.println("Through Break:");
        for (int i = 0; i < 6; i++) {

            if (i == 1) {
                break;
            } else {
                System.out.println(array[i]);
            }
        }
    }
}

public class ContinueBreakMain {

    public static void main(String[] args) {

        Continue cn = new Continue();
        Break br = new Break();

        cn.disp();
        br.disp();
    }

}

Download difference break and continue statement program,click here.

Output:

difference between break and continue statement

 

In above program as we can see is we create two normal class ‘Continue’ and ‘Break’ and another main class ‘ContinueBreakMain’. We create object for both the class and invoked disp() method through it’s respective object reference. In case of continue, if condition will true then it will execute  continue statement and skip that iteration and jump to the next iteration whereas in break when break statement execute it directly comes out from whole loop and remaining iteration will not be execute.

I hope now you have clear the difference between break and continue statement in java.

Thank you.

Stay Connected with Place4Java and follow on Facebook Page.

Be the first to comment

Leave a Reply

Your email address will not be published.


*