Break and continue are control statements. Their meanings, usage methods, and precautions are as follows:
1. Break statement: describes the application scope of the statement, that is, the break statement only makes sense in the selected structure (switch) and loop structure (while,; break indicates "Bounce". Therefore, this statement is used to jump out of the current selected structure or loop structure and is not followed by the action in the execution structure.
2. Continue statement: it also describes the application scope of the statement. The continue statement is only meaningful in the loop structure. It indicates "continue". Its function is to stop the current loop and continue the next loop.
3. Usage considerations:
- If these two statements are not used in the application scope, the existence of these statements is meaningless. If they are not used in the specified range, an error is returned, therefore, it is only used in the supporting selection structure or loop structure;
- When these two statements are executed, the subsequent statements cannot be executed;
- When break and continue exist independently, the following statements cannot be executed, so an error is reported during compilation;
- The appearance of the label allows the two statements to apply to the specified range. The label can only be used in a loop. It is a way to name a loop. See the following example:
W: For (INT x = 1; x <= 5; X ++) {q: For (INT y = 1; y <= x; y ++) {system. out. print (y); break W; // jump out of the cycle labeled W and directly end the cycle} system. out. println ();}
Example of a continue statement:
For (INT x = 0; x <= 10; X ++) {If (X % 2 = 0) continue; // the even number jumps out and ends the loop, continue the next cycle of system. out. print (x); // print if the odd number is specified}
Running result output: 13579
When break and continue exist independently, the following statements cannot be executed. An error is reported during compilation, as shown in the following example:
For (INT x = 0; x <= 10; X ++) {// If (X % 2 = 0) // if this line is commented out, an error is returned; // even number jumps out to end the loop and continues the next loop system. out. print (x); // print if the odd number is specified}
Compilation error message: the system. Out. println (x) statement cannot be accessed; // print if the odd number is displayed.
This means that when continue exists independently, the statement following it cannot be executed.