Java Basics---Break of the Java looping jump statement (25)

Source: Internet
Author: User

Break of the Java loop jump statement

In life, we often interrupt the scheduled task for some reason. If the 10000-metre long run, only ran 500 meters due to lack of physical strength, need to quit the game. In Java, we can use the break statement to exit the specified loop and execute the code immediately after the loop.

For example, use a numeric value that loops out 1--10, where the value is greater than 2, and a multiple of 3 stops the output.

Implementation code: 650) this.width=650; "style=" width:340px; src= "Http://img.mukewang.com/536b36a80001ed4b05420273.jpg"/>

Operation Result: 650) this.width=650; "src=" http://img.mukewang.com/536b36d70001a8f200670071.jpg "/>


Code:

public class HelloWorld {
public static void Main (string[] args) {

Save Cumulative value
int sum = 0;

From 1 cycles to 10
for (int i = 1; i <=; i++) {

Accumulate sum each time you loop
sum = sum + i;

Determines whether the accumulated value is greater than 20 and exits the loop if the condition is met
if (Sum > 20) {

System.out.print ("The current cumulative value is:" + sum);

Exit loop
Break
}
}
}
}


Operation Result:

The current cumulative value is: 21


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547771

Java Basic---Break of the Java looping jump statement (25)

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.