BreakContinue.java
// BreakContinue.java - example of break and continue import tio *;. Class BreakContinue {public static void main (String [] args) {int n; while (true) {// seemingly an infinite loop System.out.print ( "Enter a posital"); System.out.Print ("OR 0 to EXIT:"); n = console.in.readint (); if (n == 0) Break; // EXIT LOOP IF N IS 0 IF (n <0) Continue; // Wrong value system.out.print ("Squareroot of" n); system.out.println ("=" math.sqrt (n)); // Continue Land Here At End of current ity} // Break lands here system.out.println ("a zero was entered");}}
The BREAK statement will override the control flow from the most internal cycle, which will also cause the Switch statement to terminate. The Continue statement will result in the current iteration of the loop, and start the next iteration immediately. Continue can only appear in the For, WHILE and DO loops. Both the Break and Continue statements can be considered a limited GOTO statement, and other structured control structures can be exchanged with most places of Break and Continue, for example: NobreakContinue.java