Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Java - Break statement in java

 
 

Share on Google+Share on Google+

Java - Break statement in java

Advertisement
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 
  2.The
continue statement
  3.The
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)
  {
  
  break;
  }
  }
  if(i == j)
  {
  System.out.print(
"  " + i);
  }
  }
  }
}

Output of the program :

C:\chandan>javac Break.java

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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 6, 2007

Related Tutorials

Discuss: Java - Break statement in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
Wolf_22
August 18, 2011
Ironic...

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?
anoop
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.
DMCA.com