Road Java based learning, - notes day5

Category cycle 

for loop; while loop; do whlie cycle

 

for loop

for (initialization statement; conditional statement; conditional control statements) {

      loop statement:

}

(1) initialization statement;

(2) conditional statement; If true, the loop performed; if false, the for loop. (True or false)

(3) loop;

(4) conditional control statements; go back to (2) false until the end.

Question 1: Can the summation variables defined in the inner loop?

        Reason 1: entering each loop, sum variables are reset to zero; previously accumulated will be lost.

        Reason 2: speaking from the grammatical level, within the defined variables, will disappear from memory at the end of the cycle.

Solve the drawbacks of the for loop:

Initialization statement can be written on the outside of the for loop, the loop is finished, the initialization statement variables can also be used.

int i = 0;
for(; i<=5; i++){
    System.out.println("*");
}

for complex

A statement can define more than one variable using comma;

for(int a=0,b=0; a<=10 && b<=10; a+=2, b+=2 ){
    System.out.println(a+"111"+b);
}

Digit: 10% using the value;

Ten digits: using a numerical / 10 10%;

Hundreds digit: using a numerical / 10 1010%;

Thousands place: using a numerical / 10/10/10 10%

 

while loop

 

while (conditional statement) {

    Loop statement;

}

Complete the form:

(1) initialization statement;

while ((2) conditional statement) {

   (3) loop statement;

  (4) conditional control statements;

}

(1) -> (2) -> (3) -> (4) -> (2) -> (3) -> (4) Yes (2) is false end;

 

Note: while (ture); for (;;); infinite loop

 

initialization;

do {

Loop statement;

Conditional control statements;

} While (conditional statement); // do while at least once

 

Three differences:

for while and are the first to determine whether the establishment of a conditional statement, before deciding to execute the loop body. do while execution is the first time in the judgment.

And while the conditions for the control variable increment, for while loop is outside the grammatical structure, while the end of the loop can continue to use.

 

 

Infinite loop:

for(;;){}

while(true){}

do{}while(true);

The code can not be executed after the death cycle

Out of the infinite loop break; continue;

Published 12 original articles · won praise 2 · Views 1944

Guess you like

Origin blog.csdn.net/ytzang/article/details/104358852
Recommended