- Java Break and Continue
- Example
- Java Continue
- Example
- Break and Continue in While Loop
- Break Example
- Continue Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- Branching Statements
- The continue Statement
- The return Statement
- Java break Statement
- How break statement works?
- Example 1: Java break statement
- Example 2: Java break statement
- Java break and Nested Loop
- Labeled break Statement
Java Break and Continue
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to «jump out» of a switch statement.
The break statement can also be used to jump out of a loop.
This example stops the loop when i is equal to 4:
Example
for (int i = 0; i < 10; i++) < if (i == 4) < break; >System.out.println(i); >
Java Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
This example skips the value of 4:
Example
for (int i = 0; i < 10; i++) < if (i == 4) < continue; >System.out.println(i); >
Break and Continue in While Loop
You can also use break and continue in while loops:
Break Example
Continue Example
int i = 0; while (i < 10) < if (i == 4) < i++; continue; >System.out.println(i); i++; >
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Branching Statements
The break statement has two forms: labeled and unlabeled. You saw the unlabeled form in the previous discussion of the switch statement. You can also use an unlabeled break to terminate a for , while , or do-while loop, as shown in the following BreakDemo program:
class BreakDemo < public static void main(String[] args) < int[] arrayOfInts = < 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 >; int searchfor = 12; int i; boolean foundIt = false; for (i = 0; i < arrayOfInts.length; i++) < if (arrayOfInts[i] == searchfor) < foundIt = true; break; > > if (foundIt) < System.out.println("Found " + searchfor + " at index " + i); >else < System.out.println(searchfor + " not in the array"); >> >
This program searches for the number 12 in an array. The break statement, shown in boldface, terminates the for loop when that value is found. Control flow then transfers to the statement after the for loop. This program’s output is:
An unlabeled break statement terminates the innermost switch , for , while , or do-while statement, but a labeled break terminates an outer statement. The following program, BreakWithLabelDemo , is similar to the previous program, but uses nested for loops to search for a value in a two-dimensional array. When the value is found, a labeled break terminates the outer for loop (labeled «search»):
class BreakWithLabelDemo < public static void main(String[] args) < int[][] arrayOfInts = < < 32, 87, 3, 589 >, < 12, 1076, 2000, 8 >, < 622, 127, 77, 955 >>; int searchfor = 12; int i; int j = 0; boolean foundIt = false; search: for (i = 0; i < arrayOfInts.length; i++) < for (j = 0; j < arrayOfInts[i].length; j++) < if (arrayOfInts[i][j] == searchfor) < foundIt = true; break search; >> > if (foundIt) < System.out.println("Found " + searchfor + " at " + i + ", " + j); >else < System.out.println(searchfor + " not in the array"); >> >
This is the output of the program.
The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.
The continue Statement
The continue statement skips the current iteration of a for , while , or do-while loop. The unlabeled form skips to the end of the innermost loop’s body and evaluates the boolean expression that controls the loop. The following program, ContinueDemo , steps through a String , counting the occurrences of the letter «p». If the current character is not a p, the continue statement skips the rest of the loop and proceeds to the next character. If it is a «p», the program increments the letter count.
class ContinueDemo < public static void main(String[] args) < String searchMe = "peter piper picked a " + "peck of pickled peppers"; int max = searchMe.length(); int numPs = 0; for (int i = 0; i < max; i++) < // interested only in p's if (searchMe.charAt(i) != 'p') continue; // process p's numPs++; >System.out.println("Found " + numPs + " p's in the string."); > >
Here is the output of this program:
To see this effect more clearly, try removing the continue statement and recompiling. When you run the program again, the count will be wrong, saying that it found 35 p’s instead of 9.
A labeled continue statement skips the current iteration of an outer loop marked with the given label. The following example program, ContinueWithLabelDemo , uses nested loops to search for a substring within another string. Two nested loops are required: one to iterate over the substring and one to iterate over the string being searched. The following program, ContinueWithLabelDemo , uses the labeled form of continue to skip an iteration in the outer loop.
class ContinueWithLabelDemo < public static void main(String[] args) < String searchMe = "Look for a substring in me"; String substring = "sub"; boolean foundIt = false; int max = searchMe.length() - substring.length(); test: for (int i = 0; i > foundIt = true; break test; > System.out.println(foundIt ? "Found it" : "Didn't find it"); > >
Here is the output from this program.
The return Statement
The last of the branching statements is the return statement. The return statement exits from the current method, and control flow returns to where the method was invoked. The return statement has two forms: one that returns a value, and one that doesn’t. To return a value, simply put the value (or an expression that calculates the value) after the return keyword.
The data type of the returned value must match the type of the method’s declared return value. When a method is declared void , use the form of return that doesn’t return a value.
The Classes and Objects lesson will cover everything you need to know about writing methods.
Java break Statement
While working with loops, it is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression.
In such cases, break and continue statements are used. You will learn about the Java continue statement in the next tutorial.
The break statement in Java terminates the loop immediately, and the control of the program moves to the next statement following the loop.
It is almost always used with decision-making statements (Java if. else Statement).
Here is the syntax of the break statement in Java:
How break statement works?
Example 1: Java break statement
class Test < public static void main(String[] args) < // for loop for (int i = 1; i System.out.println(i); > > >
In the above program, we are using the for loop to print the value of i in each iteration. To know how for loop works, visit the Java for loop. Here, notice the statement,
This means when the value of i is equal to 5, the loop terminates. Hence we get the output with values less than 5 only.
Example 2: Java break statement
The program below calculates the sum of numbers entered by the user until user enters a negative number.
To take input from the user, we have used the Scanner object. To learn more about Scanner , visit Java Scanner.
import java.util.Scanner; class UserInputSum < public static void main(String[] args) < Double number, sum = 0.0; // create an object of Scanner Scanner input = new Scanner(System.in); while (true) < System.out.print("Enter a number: "); // takes double input from user number = input.nextDouble(); // if number is negative the loop terminates if (number < 0.0) < break; >sum += number; > System.out.println("Sum block-inject-1" >