4 ways to interrupt loops in Java, and some questions about labeling

Source: Internet
Author: User

1.break: Jump out of the current loop and continue execution of the statement below the loop body;

2.break+ marking: Jumping out of the current loop and jumping out of the circle indicated by the label;

3.continue: Jump out of this cycle, continue to execute the loop body;

4.continue+ marking: Jump out of this cycle, and point to the position of the label, that is, once again executed in the label statement;

5. The label must be in front of a loop, and there can be no statement behind the line;

6.java with the marking of break, continue, similar to the use of Goto;

7. Put a simple example:

Package Pak;

Import java.io.*;


public class Printmenu {
public static void Main (String args[]) {
InputStreamReader ir=new InputStreamReader (system.in);
BufferedReader br=new BufferedReader (IR);
int select=0;
No1:while (True) {
System.out.println ("====================");
System.out.println ("1. Add");
System.out.println ("2. Modify");
System.out.println ("3. Delete");
System.out.println ("4. Enquiry");
System.out.println ("5. Exit");
System.out.println ("====================");
SYSTEM.OUT.PRINTLN ("Please select Menu function:");
try {
Select=integer.parseint (Br.readline ());
} catch (Exception e) {
Todo:handle exception
}
if (select<1 | | select>5) {
System.out.println ("The feature you have selected does not exist, please re-select!") ");
Continue
}
Switch (SELECT) {
Case 1:
System.out.println ("The feature you have selected is new! ");
Break
Case 2:
System.out.println ("The feature you have selected is modified!") ");
Break
Case 3:
System.out.println ("The feature you have selected is deleted!") ");
Break
Case 4:
SYSTEM.OUT.PRINTLN ("The function you choose is query!") ");
Break
Default
System.out.println ("The feature you chose is to exit!") ");
Break No1;
}
}
}


}

8. Part of their own understanding, partial reprint, link site: http://www.cnblogs.com/ycoe/archive/2006/06/02/415825.aspx;http://tech.ccidnet.com/art/ 3741/20050922/533987_1.html

4 ways to interrupt loops in Java, and some questions about labeling

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.