The break statement in Java is used to terminate a loop or switch statement prematurely. It allows the program to exit the loop or switch block when a certain condition is met.
In a loop, if a specific condition is satisfied, and you want to exit the loop immediately, you use the break statement.
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
In this example, the loop will print numbers from 0 to 4. When i becomes 5, the break statement will execute, terminating the loop.
Console Output:
0 1 2 3 4
The continue statement in Java is used to skip the current iteration of a loop and proceed to the next iteration. It can be particularly useful when you want to skip over certain values in a loop.
In a loop, if you want to skip the execution for a particular condition but continue with the next iterations, you use the continue statement.
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
In this example, the loop will print odd numbers between 0 and 9. When i is even, the continue statement skips the remaining code inside the loop for that iteration.
Console Output:
1 3 5 7 9
When dealing with nested loops, the break statement only exits the loop in which it is placed. To exit multiple loops, additional logic is required.
Break is useful in nested loops when you want to exit from the innermost loop based on a condition.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
break;
}
System.out.println("i: " + i + ", j: " + j);
}
}
In this example, the inner loop breaks whenever i equals j, so it only prints pairs where i is not equal to j.
Console Output:
i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 i: 2, j: 0 i: 2, j: 1
The continue statement in nested loops will only skip the current iteration of the innermost loop where it is placed.
Continue is useful when you want to skip certain iterations of an inner loop but continue executing the outer loop.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
continue;
}
System.out.println("i: " + i + ", j: " + j);
}
}
In this example, the inner loop skips printing when i equals j, so it only prints pairs where i is not equal to j.
Console Output:
i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 i: 1, j: 2 i: 2, j: 0 i: 2, j: 1
The break statement can be used with labels to terminate outer loops. This is useful when you want to exit multiple nested loops at once.
Labels are particularly useful when you want to break out of a loop that is not the innermost one.
outerLoop:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i + j == 3) {
break outerLoop;
}
System.out.println("i: " + i + ", j: " + j);
}
}
In this example, the label 'outerLoop' allows the break statement to exit both loops when i + j equals 3.
Console Output:
i: 0, j: 0 i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 i: 1, j: 1
The continue statement can also be used with labels to skip iterations of outer loops. This is useful for skipping the rest of the current iteration of a labeled loop.
Labels with continue are useful when you want to skip the current iteration of an outer loop.
outerLoop:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
continue outerLoop;
}
System.out.println("i: " + i + ", j: " + j);
}
}
In this example, the label 'outerLoop' allows the continue statement to skip to the next iteration of the outer loop when i equals j.
Console Output:
i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 i: 2, j: 0 i: 2, j: 1
The break statement is used in switch statements to terminate a case and prevent fall-through to subsequent cases.
In switch statements, break is crucial to ensure that only the matched case is executed.
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
In this example, when day is 2, the program prints "Tuesday" and exits the switch block due to the break statement.
Console Output:
Tuesday
The continue statement can be used in for-each loops to skip the current iteration and move to the next element.
In for-each loops, continue is useful when you want to skip processing certain elements.
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
if (num % 2 == 0) {
continue;
}
System.out.println(num);
}
In this example, the loop prints only the odd numbers in the array, skipping the even numbers due to the continue statement.
Console Output:
1 3 5
The break statement can be used in while loops to exit the loop when a specific condition is met.
Break in while loops is useful to stop the loop based on dynamic conditions.
int i = 0;
while (i < 10) {
if (i == 5) {
break;
}
System.out.println(i);
i++;
}
In this example, the loop prints numbers from 0 to 4. When i becomes 5, the break statement terminates the loop.
Console Output:
0 1 2 3 4
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies