Share on Google+Share on Google+

Java Break while example

Advertisement
Break is often used in terminating for loop but it can also be used for terminating other loops as well such as while, do while etc.

Java Break while example

     

Break is often used in terminating for loop but it can also be used for terminating other loops as well such as while, do while etc. .
Break is a tool inside Java branching category. With the example below, how to terminate the while loop is demonstrated. 
In the program we breaking while loop under label mass.

 

 

 

 

 

Break While Loop Java

import javax.swing.JOptionPane;

public class Java_Break_While {
public static void main(String args[]) {

String str = "stop", choice = "", strb = "";
boolean value = true;
String value0 = "true";

mass:
while (value) {

choice = JOptionPane.showInputDialog(null, "Type 'stop'", "Input box ", 1);
strb = JOptionPane.showInputDialog(null, "Type boolean 'true'", "Input box ", 1);

if(choice.equals(str) && strb.equals(value0)){
JOptionPane.showMessageDialog(null, "Well ! now you have break the \n\t\twhile loop \n\t\tSuccessfully", "Congrats ", 1); 
break mass;
}
else
JOptionPane.showMessageDialog(null, "Type correct value", "Alert ", 1); 

}

}
}

Download the example

Advertisements

Advertisement

Posted on: November 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Java Break while example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0