What is difference between break statement and continue statement

What is difference between break statement and continue statement ?

Answer : break statement use to break the loop and move to next step while continue jumps over to the next iteration ( There are two types of continue statement can be used i.e. Labled or Unlabeled statement).Please see example below:

public class BreakContinueTest {

    public static void main(String[] args) {

	System.out.println("break Test");

	for (int i = 0; i < 4; i++) {
	    System.out.println("Value of i: " + i);
	    if (i == 2)
		break;
	}

	System.out.println("\nUnlabled continue Test");
	boolean dummyValue=true;
	for (int j = 0; j < 4; j++) {
	    System.out.println("Value of j: " + j);
	    if (dummyValue)
		continue;
	}

	System.out.println("\nlabled continue Test");
	outerloop:
	for (int j = 0; j < 3; j++) {

	    for (int i = 0; i < 3; i++) {

		if (i==2)
		continue outerloop;
		System.out.println("Value of j: " + j);
	    }

	}
    }

}

 

Output:

What is difference between break statement and continue statement

Leave a Reply

Your email address will not be published. Required fields are marked *