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