Daily sentence: to put it down is to move forward better. The former beauty is kept in the bottom of my heart, and the former sadness is put behind my mind. The past is always the past. That person, that thing and that feeling are just the clouds of the past.
while( Boolean expression ) { //Cyclic content }
As long as the Boolean expression is true, the loop will continue to execute.
[illustration] at the beginning of the cycle, the accounting is calculated once " Boolean expression " If the condition is true, the loop body is executed. For each additional cycle later, it will be recalculated before the start to judge whether it is true. Until the condition does not hold, the cycle ends. In most cases, we will stop the loop. We need a way to invalidate the expression to end the loop.The methods are: circulating internal control and setting flag bits externally! etc.
public static void main(String[] args) { int i = 0; //If i is less than 100, it will cycle all the time while (i<100){ i++; System.out.println(i); } }
In a few cases, the loop needs to be executed all the time, such as the server's request response listening, etc.
public static void main(String[] args) { while (true){ //Waiting for client connection //Timing check //...... } }The cycle condition is always true It will cause infinite loop [dead loop]. We should try to avoid dead loop in normal business programming. Will affect program performance or cause The program is stuck and running!
[case: calculation 1 + 2 + 3 +... + 100 =?]
public static void main(String[] args) { int i = 0; int sum = 0; while (i <= 100) { sum = sum+i; i++; } System.out.println("Sum= " + sum); }2. Do... While loop about while Statement, if the condition is not met, the loop cannot be entered. But sometimes we need to perform at least once even if the conditions are not met.
The do... While loop is similar to the while loop, except that the do... While loop is executed at least once.
do { //Code statement }while(Boolean expression);
Note: the Boolean expression follows the loop body, so the statement block has been executed before detecting the Boolean expression. If the Boolean expression has a value of true , the statement block executes until the value of the Boolean expression is false .
We use do...while Transform the above case!
public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum+i; i++; }while (i <= 100); System.out.println("Sum= " + sum); }The execution result is of course the same!
While and do-While Differences between: While is judged before execution. Do while is to execute first and then judge! Do...while always ensures that the loop is executed at least once! This is their main difference.
public static void main(String[] args) { int a = 0; while(a<0){ System.out.println(a); a++; } System.out.println("-----"); do{ System.out.println(a); a++; } while (a<0); }
3.For cycle Although all loop structures can be used while perhaps do...while Means, but Java Another statement is provided - for Loop, making some loop structures simpler. for Loop statement is a general structure that supports iteration. It is the most effective and flexible loop structure. for The number of times the loop is executed is determined before execution. The syntax format is as follows:
for(initialization; Boolean expression; to update) { //Code statement }
There are the following explanations for the for loop:
1. Perform the initialization step first. You can declare a type, but you can initialize one or more loop control variables or empty statements.
2. Then, detect the value of the Boolean expression. If true, the loop body is executed. If false, the loop terminates and the loop begins to execute The statement after the ring body. 3. After executing a cycle, update the cycle control variable (the iteration factor controls the increase or decrease of the cycle variable). 4. Detect the Boolean expression again. Loop through the above procedure. [Demo: while and for [output]public static void main(String[] args) { int a = 1; //initialization while(a<=100){ //Conditional judgment System.out.println(a); //Circulating body a+=2; //Iteration} System.out.println("while End of cycle!"); for(int i = 1;i<=100;i++){ //Initialize / / condition judgment / / iteration System.out.println(i);//Circulatory body } System.out.println("while End of cycle!"); }We found that, for Loop simplifies the code and improves readability when the number of cycles is known. What we usually use most is our for Cycle! 4. Practice [practice] 1 : Calculation 0 reach 100 Between odd and even]
public class java1 { public static void main(String[] args) { int i = 0; int j = 0; for (int i1 = 100; i1 > 0; i1--) { if (i1%2==0){ i += i1; }else{ j += i1; } } System.out.println("Cardinality sum"+j); System.out.println("Even sum"+i);[practice] 2 : use while or for Cyclic output 1-1000 Can be 5 The number of integers divided, and each line is output 3 Number]
public static void main(String[] args) { for(int j = 1;j<=1000;j++){ if(j%5==0){ System.out.print(j+"\t"); } if(j%(5*3)==0){ System.out.println(); } } }[practice] 3 : print 99 multiplication table]
public class java1 { public static void main(String[] args) { for (int i = 1 ; i < 10 ;i++) { for (int i1 = 1; i1 < i+1; i1++) { System.out.print(i+"*"+i1+"="+i*i1+"\t"); } System.out.println(); }5.break & continue
1. break keyword
break Mainly used in loop statements or switch Statement, used to jump out of the entire statement block. break Jump out of the innermost loop and continue to execute the statements below the loop. [Demo: jump out of loop]public class java1 { public static void main(String[] args) { for (int i1 = 100; i1 > 0; i1--) { if (i1==80){ break; }else{ System.out.println(i1); } }
Output:
2. continue keyword
continue Applicable to any cycle control structure. The function is to make the program jump to the iteration of the next loop immediately. stay for In the cycle, continue Statement causes the program to immediately jump to the update statement. stay while perhaps do...while In the loop, the program immediately jumps to the judgment statement of the Boolean expression. [demonstration]public class java1 { public static void main(String[] args) { for (int i1 = 0; i1 < 10; i1++) { if (i1==5){ continue; }else{ System.out.println(i1); } }
Output:
We can see no 5.
3. The difference between the two
break Available in the body of any loop statement break Control the flow of the cycle. break Used to forcibly exit the loop without executing the remaining statements in the loop. (break The statement is also switch Used in statements ) continue Statement is used in the body of a loop statement to terminate a loop process, that is, skip the statements that have not been executed in the loop body, and then determine whether to execute the loop next time.