Java break statement with Example | Java break keyword

A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. Java control statements can be put into the following three categories: selection, iteration, and jump. In this tutorial, we will learn about the Java jump statement. The jump statement can be used to transfer the control to other parts of the program. Java break statement is one of the jump statement.


Java break statement:

Java break statement can be used to terminate the loop. It can be used inside a loop. We can use a break statement in all types of loops such as: while loop, do-while loop, and for loop. When a break statement is encountered inside a loop, the loop is terminated and the program control goes to the next statement following the loop. In java, the break statement has three uses.

1. It terminates a statement sequence in a switch statement.
2. It can be used to exit a loop.
3. It can be used as a "civilized" form of goto (labeled break).



Syntax of Java break statement:

 break; 


Flowchart of Java break statement:

Java break statement



Using the break to exit a loop:

When a break statement is encountered inside a loop, the loop is terminated and the program control goes to the next statement following the loop. See the example below:


Java break statement example:


class BreakLoop{
	public static void main(String args[]){
		for(int i = 1; i<=10; i++){
			if(i==6)
				break;
			System.out.println("i is: " +i);
		}
		System.out.println("Loop Complete !");
	}
}


Output:
Java break statement


Java labeled break statement.

The break statement can also be used by itself to provide a "civilized" form of the goto statement. Java doesn't provide a goto statement, because it provides a way to branch in any arbitrary and unstructured manner. Java uses the label. A label is a block of code that must enclose the break statement, but it doesn't need to be immediately enclosing block. This means that you can use a labeled break statement to exit from a set of nested blocks. 



Java labeled break statement example:


class LabeledBreakLoop{
	public static void main(String args[]){
		boolean b = true;
		
		// first lable.
		first:{
			//second lable.
			second:{
				//third label.
				third:{
					System.out.println("Before the break statement.");
					if(b)
						break second;
					System.out.println("This would not execute! ");
				}
				System.out.println("This would not execute! ");	
			}
			System.out.println("This is after the second block.");
		}
	}
}


Output:

Before the break statement.
This is after the second block.



you may also like these related posts:

1. How to set environmental path in java.

Java break statement with Example | Java break keyword Java break statement with Example | Java break keyword Reviewed by Prashant Srivastava on December 20, 2019 Rating: 5

No comments:

Powered by Blogger.