Java break and continue source resolution _java

Source: Internet
Author: User

In the process of learning the Java language yourself, it is easy to confuse the use of break and continue. In order to facilitate quick access and review, this special stay to learn a note.
Brief
in the main part of any iteration statement, you can use break and continue to control the loop flow. Where the break is used to force out the loop and does not execute the remaining statements in the loop. Instead, continue stops executing the current iteration and then returns to the beginning of the loop and begins the next iteration.
Source Code
The following procedure shows you the examples of break and continue in the for and while loops:

Package com.mufeng.thefourthchapter;

public class Breakandcontinue {public
 static void Main (string[] args) {for
 (int i = 0; i < i++) {
  if (i = = 74) {//out of For loop break
  ;
  }

  if (i% 9!= 0) {//Next iteration
  continue;
  }
  System.out.print (i + "");
 }
 System.out.println ();

 int i = 0;
 while (true) {
  i++;
  Int j = i *;
  if (j = = 1269) {//out of the loop break
  ;
  }

  if (i%!= 0) {//Top of Loop
  continue;
  }
  System.out.print (i + "");}}

 

Output results
01.0 9 18 27 36 45 54 63 72
02.10 20 30 40
Source Analysis
In this for loop, the value of I will never reach 100, because once I arrives at the 74,break statement, it interrupts the loop. Typically, you use a break only if you do not know when the interrupt condition will be met. As long as I cannot be divisible by 9, the Continue statement causes the execution to return to the very beginning of the loop (which increments the I value). If it is divisible, the value is displayed. The output shows 0 because the 0%9 equals 0.
Finally, you can see an "infinite while loop" situation. However, there is a break statement inside the loop, which aborts the loop. In addition, you will see that the continue statement execution sequence moves back to the beginning of the loop without completing the contents of the Continue statement. (The value is printed only if I can be divisible by 10.) )

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.