Break and Continue

This tutorial is more about to get deep knowledge of Break and continue. Break statement terminates the loop as soon as it executes. Continue statement will allow loop to jump to the next iteration.

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

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

πŸ“ Blogs

πŸ“§ Imran Khan