
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Break, Continue and Label in Java Loop
In this article. we will learn about break, continue, and label in Java loop. There are cases when you would want to manage the flow of your loop, that is, skip some iterations or terminate the loop altogether. This is where break, continue, and label statements are used.
Break Statement
A break statement interrupts the normal execution of a loop and causes it to exit before being completed. The loop will terminate and the flow of control will transfer to the next statement immediately following the loop, when the break function is called. It can be used in any loop statement, whether for, while, or do-while loop statements.
Example
Below is an example of a break statement in Java ?
for (int numval = 0; numval < 15; numval++) { if (numval == 7) { break; } System.out.println(numval ); }
The loop will print out the numbers 0 through 6 and then break when the value of numval reaches 7.
Continue Statement
The continue statement is applied to bypass the current iteration of a loop and proceed to the next iteration. The program will bypass the rest of the code in the current iteration once the continue statement is reached.
Example
Below is an example of a continue statement in Java ?
int numval = 0; while ( numval < 10) { numval++; if ( numval == 3) { continue; } System.out.println(numval ); }
While loop will print the numbers 1 through 4, skip the number 5, and then print the numbers 6 through 10.
Label Statement
The label statement is used to identify a specific loop or statement in your code. Labels are useful when you need to use the break or continue statement to control the flow of a nested loop.
Example
Below is an example of a label statement in Java ?
second: for (int i = 0; i < 3; i++) { for (int j = 0; j< 3; j++){ if(i == 1){ break second; } System.out.print(" [i = " + i + ", j = " + j + "] "); } }
When the break statement is encountered, the program will exit the for loop.
The following example showcases labels 'first', and 'second' before a for statement and uses the break/continue controls to jump to that label. See the example below.
Example
Below is the combined example for the given statement in Java ?
public class Tester { public static void main(String args[]) { first: for (int i = 0; i < 3; i++) { for (int j = 0; j< 3; j++){ if(i == 1){ continue first; } System.out.print(" [i = " + i + ", j = " + j + "] "); } } System.out.println(); second: for (int i = 0; i < 3; i++) { for (int j = 0; j< 3; j++){ if(i == 1){ break second; } System.out.print(" [i = " + i + ", j = " + j + "] "); } } } }
first is the label for the first outermost loop and the continue causes the for-loop to skip the print statement if i = 1. The second is the label for the second outermost for-loop and continue second causes the loop to break the loop.