10- and out of the loop

while circulation 
loop is to let the computer do the cycle calculated according to the conditions, the cycle continues when the conditions are met, the loop exits when the condition is not satisfied. 
While Java provides conditions for cycling. Its basic usage is 
the while (conditional expression) { 
    loop 
} 

the while loop before the start of each cycle, first determine whether the conditions established. If the result is true, put the loop body statement is executed again, if evaluates to false, it directly out of the while loop, continue down the implementation. 
For example: 
// incremented by 1 to 100 
        int SUM = 0 ; // accumulated and, initialized to 0 
        int n-= . 1 ;
         the while (n-<= 100 ) { // cycle that n-<= 100 
            SUM = SUM n-+; // the sum accumulated into the n 
            n ++; // n. 1 itself plus 
        } 
        the System. OUT.println (SUM); // 5050 

Note: while loop is to determine the loop condition, recycling, therefore, it is possible to cycle nothing. Cycling conditions for determination, and a processing increment variable, paying particular attention to boundary conditions. If the loop condition is always met, and that this cycle becomes an infinite loop. 
/ * 
    A very interesting cycle 
    on the surface of n will always> 0, the condition has been established 
    , however, int type has a maximum value, the maximum accumulated into n -1 becomes 
    the cycle is out 
* / 
        int n = . 1 ;
         the while ( n-> 0 ) { 
            SUM = SUM + n-; 
            n- ++ ; 
        } 

do the while loop 
in Java, the while loop is the first loop condition is determined, then execution cycle. While another do while loop is executed first cycle, and then determine the conditions to continue the cycle when conditions are met, quit when the condition is not satisfied, then at least do while loop will cycle once 
do { 
    perform loop 
} the while(Conditional expression); 


for loop 
for loop is very powerful, it uses the counter to achieve the cycle. for loop counter initialized, then, at each cycle prior to the detection loop condition update counter after each cycle. Usually named counter variable i. 
for (initial conditions; cycle testing conditions; update counter of the cycle) {
     // execute statement 
} 

in the loop is executed for the front, first performs initialization statement, then, check the loop condition before the loop, the loop counter is updated, in a loop for the circulating inside of the body, it does not need to update counter value. 
// for calculating loop arrays and 
        int [] = {NS . 1 , . 4 , . 9 , 16 , 25 };
         int SUM = 0 ;
         for ( int I = 0 ; I <ns.length; I ++ ) { 
            . The System OUT . the println ( " I =" + I + " , NS [i] = " + NS [i]); 
            SUM = SUM + NS [i]; 
        } 

Note: When using a for loop, the counter variable i to try to define in a for loop, following the access range reduced to the minimum principle. 
in some cases, may be omitted for loop initialization statements, loops, and each loop update statements conditions, but is not recommended write. 
// no end conditions: 
for ( int I = 0 ;; I ++ ) { 
    ... 
} 
// no end conditions and update statements: 
for ( int I = 0 ;;) { 
    ... 
} 
// nothing is provided: 
for (;;) { 
    ... 
} 


for each cycle
Java also provides an alternative for each loop, can more easily traverse the array, however, for the each loop traversal order can not be specified, you can not get indexed array. In addition to an array of external, for each loop can traverse all "available iterations" data types, including List, Map like
         int [] NS = { . 1 , . 4 , . 9 , 16 , 25 };
         for ( int n-: NS) { 
            the System. OUT .println (n-); 
        } 


break 
either while loop or for loop can break out of the loop with the statement and the continue statement 
break statement usually with if statements. Pay special attention to, break out of that one statement is always where their circulation. 
For example: 
        for ( int I = . 1 ; I <= 10; i++) {
            System.out.println("i = " + i);
            for (int j=1; j<=10; j++) {
                System.out.println("j = " + j);
                if (j >= i) {
                    break;
                }
            }//内层for
            // break跳到这里
            System.out.println("breaked" ); 
        } // layer for
 @ BREAK for inner loop is located, for the inner loop will pop, but not for the outer loop out         

Continue 
BREAK will cycle out of the current, i.e. the whole cycle will not be performed and continue is ahead of the end of this cycle, proceed directly to the next iteration of the nested loop, continue statement is also the end of this cycle where their.. 
        int SUM = 0 ;
         for ( int i = 1 ; I <= 10 ; I ++ ) { 
            the System. OUT .println ( " the begin = I " + I);
             IF (I% 2 == 0 ) {
                 Continue ; //continue statement will end this cycle 
            } 
            SUM = SUM + i; 
            . the System OUT .println ( " End i = " + i); 
        } 
// when i is an odd number, complete the entire cycle is performed, the print will begin i = 1 and the end i = 1.
// When i is even, continue statement ahead of the end of this cycle,
 // therefore, prints begin i = 2 but does not print the end i = 2. 


BREAK label Usage: out of multiple nested loop
 BREAK label out of the code block is defined by the label of the label. 
        Label1: // tag label statement blocks later to add: 
        the while ( to true ) {
             for ( int I = 0 ; I <= 10; i++) {
                System.out.println("i=" + i);
                if (i == 5) {
                    break label1;//跳出
                }
            }

 

Guess you like

Origin www.cnblogs.com/nsss/p/11417456.html