Mu Class Network-android engineer first form break of -4-11 Java loop jump statement

Source: Internet
Author: User

Source: http://www.imooc.com/code/1431

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:

Operation Result:

Task

Good memory is better than bad writing, practice time!

Implementation function: The loop adds the integers between 1 and 10, if the accumulated value is greater than 20, jumps out of the loop and outputs the current accumulated value.

Operation Result:

Implementation idea: Define a variable sum to save the accumulated value, define a variable I save 1 to 10 of the integer, in the loop to accumulate sum, and also determine whether the accumulated value is greater than 20, when greater than 20 o'clock output the current cumulative value and exit the loop.

Please add the statement to the 14th, 19 lines in the editor on the right.

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         4         //Save Cumulative value5         intsum = 0;6         7         //from 1 cycles to ten8          for(inti = 1; I <= 10; i++) {9             Ten             //accumulate sum each time you loop Onesum = sum +i; A              -             //determines whether the accumulated value is greater than 20 and exits the loop if the condition is met -             if (              ) { the                  -System.out.print ("The current cumulative value is:" +sum); -                  -                 //Exit Loop +                  -             } +         } A     } at}

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         4         //Save Cumulative value5         intsum = 0;6         7         //from 1 cycles to ten8          for(inti = 1; I <= 10; i++) {9             Ten             //accumulate sum each time you loop Onesum = sum +i; A              -             //determines whether the accumulated value is greater than 20 and exits the loop if the condition is met -             if(Sum > 20) { the                  -System.out.print ("The current cumulative value is:" +sum); -                  -                 //Exit Loop +                  Break; -             } +         } A     } at}

Mu Class Network-android engineer first form break of -4-11 Java loop jump statement

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.