Break and continue control statements

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.