Several ways to interrupt Java iterations

Source: Internet
Author: User

Several ways to interrupt Java iterations

This article summarizes the usage of several keywords in Java that interrupt iterations, and return does not say a word about break and continue and how to implement the GOTO keyword in other languages in Java.

    • The difference between break and continue

Both have the function of jumping out of the loop, the difference is that the break jumps out of the loop, directly terminates the for or while loop, does not perform the subsequent iterations, and continue jumps out of the loop refers to jump out of this iteration, and then the next iteration.

    • Goto Introduction
      Goto originated from the assembly language Program control, if a is set up, then jump here, otherwise jump there.
      Although Goto is a reserved keyword in Java, it is not used in the program, and there is no Goto in Java. However, Java can also do some similar jumps, which is related to the break and continue keywords, they are not jump, but rather a way to interrupt the iteration, it is included in the Goto issue is discussed together, because they all use the same mechanism: tags.

    • How to implement Goto in Java

Look at the following code, the actual implementation of the following can be found in several ways of breaking the difference:

 Public  Class interrupt Iteration {     Public Static void Main(string[] args) A for(inti =0; I <Ten; i++) { for(intj =0; J <Ten; J + +) {if(J = =5) {Continue; }if(J = =6) {ContinueA }if(i = =2) { Break; }if(i = =8) { BreakA } System.out.println ("i="+ i +", j=", M); }        }    }}
    • Summary
      1, break is to interrupt the internal iteration, back to the external iteration.
      2. Continue moves the execution point back to the beginning of the internal iteration.
      3, continue A is to interrupt both internal and external iterations, go directly to a, and then continue to start the iterative process from an external iteration.
      4. Also interrupts all iterations and returns to a, but does not re-enter the iteration. That is, it actually completely terminates the two iterations.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Several ways to interrupt Java iterations

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.