The roles and differences of the two
1. Break: Jump directly out of the current loop body (while, for, do, or block (switch). When the switch case executes, it is bound to match, the match succeeds in returning the value of the current case, and then, depending on whether there is a break, whether to continue the output, or to jump out of judgment (refer to switch introduction).
2. Continue: The code after the continue statement in the loop body is no longer executed and the Next loop is performed directly .
Code Demo
public class Test {public static void Main (string[] args) { System.out.println ("-------Continue Test----------") ; for (int i = 0; i < 5; i++) { if (i = = 2) { System.out.println ("Skip output statement below, return for Loop"); Continue; } System.out.println (i); } System.out.println ("----------Break Test----------"); for (int i = 0; i < 5; i++) { if (i = = 2) { System.out.println ("Skip output statement below, return for Loop"); break; } System.out.println (i);}} }
Operation Result:
When you can see the test continue, when i==3, skip the output statement directly after continue, and go to the next loop.
In the break test, when i==2 jumps out of the for loop, no longer executes the loop after
The role of java:break and Continue keywords