The difference between break and continue in Java (attached source)

Source: Internet
Author: User

Preface to learn in self JavaThe process of language, it is easy to put BreakAnd ContinueConfusion of usage. In order to facilitate the quick review and study, in this special stay to learn a note.
A brief description of the body part of any iteration statement can be BreakAnd ContinueThe flow of control loops. which BreakUsed to forcibly exit a loop without executing the remaining statements in the loop. and ContinueStops executing the current iteration, then returns to the start of the loop and begins the next iteration. The code below this program to show you BreakAnd ContinueIn forAnd whileExamples in the loop:
Package Com.mufeng.thefourthchapter;public class Breakandcontinue {public static void main (string[] args) {for (int i = 0; I < 100; i++) {if (i = =) {//out of for Loopbreak;} if (i% 9! = 0) {//Next iterationcontinue;} System.out.print (i + "");} SYSTEM.OUT.PRINTLN (); int i = 0;while (true) {I++;int j = i * 27;if (j = = 1269) {//Out of Loopbreak;} if (i%! = 0) {//Top of Loopcontinue;} System.out.print (i + "");}}}
Output results
Source code parsing in this forCycle, IThe value will never reach -, because once I arrives About BreakThe loop is interrupted by the statement. In general, you need to use this only if you do not know when the interrupt condition is met Break。 Just ICannot be 9Divisible ContinueStatement causes the execution process to return to the beginning of the loop (which causes the Ivalue is incremented). If divisible, the value is displayed. The result of the output is displayed 0, is due to 0%9Equals 0。 Finally, you can see an "infinite whileCycle "situation. However, inside the loop there is a BreakStatement to abort the loop. In addition, you will also see ContinueThe statement execution sequence moves back to the beginning of the loop without completing ContinueThe content used after the statement. (Only in ICan be TenThe value is only printed when the integer is divisible. )


The difference between break and continue in Java (attached source)

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.