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); } } } |