< Prev
Next >



Break Statement





When a break statement is encountered within a loop of a program, it stops executing the remaining statements in the loop and takes the control out of it to execute the first statement right after the loop.




Note :


Break statement must be inside a loop, otherwise a compile error is issued by Java Compiler at the time of compilation of the program..







Break statement example


//Break statement example 

class Break         //class starts
{
public static void main(String... ar)      //main method starts
{

for(int i=0;i<10;i++)
{
	if(i==5)
		break;	//when i is equal to 5, break statement takes the control out of loop

	System.out.println(i);
}

System.out.println("Out of the loop");

} // main method ends
} // class ends


Output


0
1
2
3
4
Out of the loop

As you can see in the program and its output, when i is equal to 5, break statement breaks the execution of the loop and it takes the control out of loop to execute the first statement outside the loop.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Do-While Loop
Continue Statement>
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2