Advertisement



< 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..





Advertisement




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 -





< Prev
Next >
< Do-While Loop
Continue Statement>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement