Java - Break statement in java

In this section you will learn how to use the break statement.

Java - Break statement in java


The java programming language supports the following types of controlling statements such as:
1.The break statement 
continue statement
return statement

Break: The break statement is used in many programming languages such as c, c++, java etc. Some times we need to exit from a loop before the completion of the loop then we use break statement and exit from the loop and loop is terminated. The break statement is used in while loop, do - while loop, for loop and also used in the switch statement.

Code of the program : 

public class  Break{
  public static void main(String[] args){
  int i,j;
  System.out.println("Prime numbers between 1 to 50 : ");
 for (i = 1;i < 50;i++ ){
  for (j = 2;j < i;j++ ){
  if(i % j == 0)
  if(i == j)
"  " + i);

Output of the program :


C:\chandan>java Break
The Prime number in between 1 - 50 :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Download this example.



We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Java - Break statement in java

Posted on: June 6, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java - Break statement in java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 18, 2011

Why on Earth would you use 2 FOR loops with modulus conditioning of single-character variables to explain BREAK statements!? (If someone comes to this website seeking an explanation about BREAK, then is it fair to assume that the person might have problems understanding your demo?)
farhat bouchnak
April 30, 2012
code understanding

hi is it right to use with if only break? please help me and how?
June 17, 2012
output not correct

public class Break{ public static void main(String[] args){ int i,j; System.out.println("Prime numbers between 1 to 50 : "); for (i = 1;i < 50;i++ ){ for (j = 2;j < i;j++ ){ if(i % j == 0) { break; } } if(i-1 == j) //THE CORRECTION { System.out.print(" " + i); } } } } how to get the 2 in output?
Rubbal Bhusri
August 25, 2013
No Correction

The code given above produces fair result. I've checked it and there isn't any need of correction in it, because it print all the prime numbers starting from 2 and ends at 47, which is, it suppose to display. So, all works good in this code.