Similar to the C ++ goto function in Java --- label Introduction

Source: Internet
Author: User

Today, when I was looking at an open-source project, I accidentally saw an unfamiliar syntax and usage. So I checked the information and wrote a demo for verification. The result is as follows:

Anyone who knows C ++ knows the word "Goto". This keyword allows us to jump flexibly in the program. Of course, it is not recommended, but he also has his specific purpose. For example, it is particularly useful when multiple loops are exceeded:

Public class main {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub

Init ();
}

Private Static void Init (){

For (Int J = 0; j <30; j ++ ){

For (int A = 0; A <30; A ++ ){

If (A = 15)
Break label;
System. Out. println ("third ====" + );
}

System. Out. println ("second ====" + J );
}

System. Out. println ("first =" + I );
}

System. Out. println ("End ==== ");

}

}

The output result is:

Third ====== 0
Third ====== 1
Third ======= 2
Third ===== 3
Third ======= 4
Third ====== 5
Third ====== 6
Third ====== 7
Third ====== 8
Third ===== 9
Third ====== 10
Third ====== 11
Third =====12
Third =====13
Third =====14
End ======

We can see that after the label is set before the recycling starts, you can use the break label in the internal loop to jump out of all the loops and then execute the code after the outgoing loop. Another usage is the continue label.

The continue label is used to jump out of all loops and return to the start of the outermost loop to re-start the execution of the circular code.

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.