Java break Statement
Overview
In this post, we will discuss the Java break statement. Sometimes we may come across situations where we want to skip loops or terminate the loop.
We can use the break statement in all the loops in Java. When a break statement is executed, the loop is terminated and the control is transferred to the next statement outside the loop.
break statement
We can use the break statement to terminate the loop immediately. The control of the program reaches to the next statement after the loop.
Let’s see with an example.
public class BreakDemo { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i < 100; i++) { if (i == 7) { break; // The loop will terminate when i reaches 7 } System.out.println("i=" +i); } } }
In the case of nested loops, the break statement terminates the innermost loop, and control jumps to the next outer loop that surrounds in the depth.
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :