Break and Continue
Break:
Break statement terminates the loop as soon as it executes. The break statement will break all kind of loops(for, while, do-while and enhanced for loop) and switch statements.
for (int i = 0; i < 4; i++) {
if (i == 2) {
break;
}
System.out.println(i);
}OUTPUT:
01
Note:
In the above example loop starts run form i value equals to 0 and get break when i value becomes 2. After break loop will not be executing any statement inside loop.
Continue:
Continue statement will allow loop to jump to the next iteration. The continue statement will be applicable for all kind of loops(for, while, do-while and enhanced for loop).
for (int i = 0; i < 4; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}OUTPUT:01
Note:
In the above example loop starts run form i value equals to 0 and print value of i. When i value becomes 2 the loop gets jump to the next iteration and don’t print value of i.
Difference between Break and Continue
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.