JAVA label and continue, break, java label continue

Source: Internet
Author: User

JAVA label and continue, break, java label continue
You can add labels to statement blocks to give them names. The labels are located before the statements. The label can only be referenced by continue and break. The format is as follows:Label: statementOnly one label can be added before the statement, and the label cannot be followed by braces. Use the break label to control the statements in the label. It is usually followed by a loop such as for. while. do-while. By using labels, we can control the external layer cyclically. The following describes how to use continue to control labels.

public class Label {    public static void main(String[] args) {        System.out.println("i j");        search:            for (int i = 0; i < 3; i++) {                for (int j = 0; j < 50; j++) {                    if (j == 3)                        continue search;                    System.out.println(i+" "+j);                }            }    }}

 

The output is as follows:

I j
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2

It can be seen that the inner loop has not been executed for 50 times, But when it reaches the continue, it jumps to the outermost loop. After the continue is executed, it executes I ++.

 

The following is a break label.

public class Label {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("i j");        search:            for (int i = 0; i < 3; i++) {                for (int j = 0; j < 50; j++) {                    if (j == 3)                        break search;                    System.out.println(i+" "+j);                }            }    }}

 

The output is as follows:

I j
0 0
0 1
0 2

 

It can be seen that the inner loop has not been executed 50 times, and the loop has never been executed again after break. Break jumps out of the outermost loop and out of the label range.

 

The break jump-out label is useful for querying a record. When you find a record you want, you can jump out of the loop without executing it again.

 

The label statement must be followed in the loop header. Label statements cannot be used before non-cyclic statements.

 

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.