Java designator and Continue,break

Source: Internet
Author: User
Tags control label

You can give the statement block designator a name that precedes the statement. Labels can only be referenced by continue and break. The format is as follows: label:statement    Only one label is allowed before the statement, and the label cannot be followed by curly braces. The statement in the label is controlled by adding a label after break. Often the label is for.while.do-while and other loops.by marking, we can control the outer layer of the loop.The following is the control label with continue
 Public classLabel { Public Static voidMain (string[] args) {System.out.println ("I J"); Search: for(inti = 0; I < 3; i++) {                 for(intj = 0; J < 50; J + +) {                    if(J = = 3)                        Continuesearch; 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 is not executed 50 times, and to the continue to jump to the outer loop, continue execution after the execution is i++.

Here is the break control label

 Public classLabel { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.println ("I J"); Search: for(inti = 0; I < 3; i++) {                 for(intj = 0; J < 50; J + +) {                    if(J = = 3)                         Breaksearch; 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 is not executed 50 times, and the loop does not execute after break. Break jumps out of the outermost loop and jumps out of the label's range.

Break out of the label is useful for querying a record, when you find a record you want, you can jump out of the loop, no more execution.

The label statement must be immediately on the head of the loop. The label statement cannot be used in front of a non-circular statement.

Java designator and Continue,break

Related Article

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.