The role of tags in the Java loop

Source: Internet
Author: User

Transferred from: http://lihengzkj.iteye.com/blog/1090034

It was not known that labels can be used in loops. After the recent encounter, there is still a unique use. What I mean by that is that the label can change the loop execution process in the loop. And this change is not what we used to use break alone or continue can achieve. Let's take a look at the example below.

Java code
  1. Outer1:
  2. for (int i =0;i<4;i++) {
  3. SYSTEM.OUT.PRINTLN ("begin to Itrate.  "+i);
  4. For (int j =0;j<2;j++) {
  5. if (i==2) {
  6. continue outer1;
  7. Break
  8. }
  9. System.out.println ("Now the value of J is:" +j);
  10. }
  11. System.out.println ("******************");
  12. }


The result of the execution is:
Begin to Itrate. 0
Now the value of J is:0
Now the value of J is:1
******************
Begin to Itrate. 1
Now the value of J is:0
Now the value of J is:1
******************
Begin to Itrate. 2
Begin to Itrate. 3
Now the value of J is:0
Now the value of J is:1
******************
Note: When i=2, continue outer1 the program back to the beginning of the Outer1 cycle, starting the next cycle, this time the execution of the loop is i=3 instead of re-starting from i=0. Also, when using continue outer1 to jump out of the inner loop, the statement behind the outer loop will not execute.    That is, at begin to Itrate. 2 There will not be a string of * numbers behind.
Contrast:

Java code
  1. Outer1:
  2. for (int i =0;i<4;i++) {
  3. SYSTEM.OUT.PRINTLN ("begin to Itrate.  "+i);
  4. For (int j =0;j<2;j++) {
  5. if (i==2) {
  6. Continue outer1;
  7. Break ;
  8. }
  9. System.out.println ("Now the value of J is:" +j);
  10. }
  11. System.out.println ("******************");
  12. }


Note: If we use break directly, we just jump out of the inner loop. As a result, you can see the difference:
Begin to Itrate. 0
Now the value of J is:0
Now the value of J is:1
******************
Begin to Itrate. 1
Now the value of J is:0
Now the value of J is:1
******************
Begin to Itrate. 2
******************
Begin to Itrate. 3
Now the value of J is:0
Now the value of J is:1
******************
-----------------------------------------------------------------Split Line
Let's look at the effect of the break+ tag again.

Java code
  1. Outer2:
  2. for (int i =0;i<4;i++) {
  3. SYSTEM.OUT.PRINTLN ("begin to Itrate.  "+i);
  4. For (int j =0;j<2;j++) {
  5. if (i==2) {
  6. Break Outer2;
  7. Break
  8. }
  9. System.out.println ("Now the value of J is:" +j);
  10. } System.out.println ("******************");
  11. }


Results:
Begin to Itrate. 0
Now the value of J is:0
Now the value of J is:1
******************
Begin to Itrate. 1
Now the value of J is:0
Now the value of J is:1
******************
Begin to Itrate. 2
Note: From the results can be seen when the i=2, the break+ tag directly to the inner and outer layers of the loop to stop. And if we use break alone, it doesn't work that way, it just jumps out of the inner loop.
Finally, the tags in Java are only suitable for use with nested loops.

The role of tags in the Java Loop (RPM)

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.