The while loop

  • While is the most basic loop, and its structure is:
While (Boolean expression){// loop content}Copy the code
  • As long as the Boolean expression is true, the loop continues.
  • We’ll stop the loop most of the time, and we need a way to end the loop by invalidating the expression.
  • There are a few cases where the loop is always executed, such as the server’s request response listening.
  • A loop condition that is always true creates an infinite loop, which should be avoided in normal business programming. Will affect the program performance or cause the program stuck crash!
  • case
package zch.struct; Public class WhileDemo02 {public static void main(String[] args) { +100 int i = 0; int sum = 0; while (i <= 100){ sum += i; i++; } System.out.println(sum); }}Copy the code

do… The while loop

  • For a while statement, if the condition is not met, the loop cannot be entered. But sometimes we need to do it at least once, even if the conditions are not met.
  • do… The while loop is similar to the while loop, except that do… The while loop is executed at least once.
Do {// code statement}while(Boolean expression);Copy the code
  • The difference between while and do-while:
    1. While determines and then executes. Do-while is execute before judge!
    2. Do-while always guarantees that the loop will be executed at least once! This is their main difference.
  • case
package zch.struct; Public class DoWhileDemo01 {public static void main(String[] args) { +100 int i = 0; int sum = 0; do { sum += i; i ++; }while (i <= 100); System.out.println(sum); }}Copy the code

The For loop

  • While all loop structures can be represented by while and do-while, Java makes some loop structures simpler by providing another statement, the for loop.
  • The for loop statement is a generic structure that supports iteration, and == is the most efficient and flexible loop structure ==.
  • The number of times the for loop is executed is determined prior to execution. The syntax is as follows:
For (int I = 0; i <= 100; I++){// code statements}Copy the code
  • = = = =

The initialization step is performed first. A type can be declared, but one or more loop control variables can be initialized, or it can be an empty statement. Then, the value of the Boolean expression is tested. If true, the loop body is executed. If false, the loop terminates and the statement following the body of the loop begins execution. After a loop is executed, the loop control variables are updated (iteration factors control how much the loop variable increases or decreases). Check the Boolean expression again. Repeat the above procedure.

//死循环
for( ; ; ){
    
}
Copy the code
  1. Case 1
package zch.struct; Public class ForDemo01 {public static void main(String[] args) {public static void main(String[] args) { // even and int evenSum = 0; for (int i = 0; i <= 100; i++) { if (i % 2 ! = 0){ oddSum += i; }else { evenSum += i; }} system.out. println(" oddSum: "+ oddSum); System.out.println(" evenSum: "+ evenSum); }}Copy the code
  1. Case 2
package zch.struct; Public class ForDemo02 {public static void main(String[] args) {public static void main(String[] args) { And each line prints three for (int I = 1; i <= 1000; i++) { if (i % 5 == 0){ System.out.print(i + "\t"); } if (i % (5 * 3) == 0){ System.out.println(); } } //2 // int i = 1; // while (i <= 1000){ // if (i % 5 == 0){ // System.out.print(i + "\t"); // } // if (i % (5 * 3) == 0){ // System.out.println(); // } // i++; // } //3 // int count = 0; // for (int i = 1; i <= 1000; i++) { // if (i % 5 == 0){ // System.out.print(i + "\t"); // count++; // } // if (count ! = 0 && count % 3 == 0){ // System.out.println(); // count = 0; //} //Copy the code
  1. Example 3: Print the multiplication table
package zch.struct; Public class ForDemo03 {public static void main(String[] args) {for (int I = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + "*" + i + "=" + (j * i) + "\t"); } System.out.println(); }}}Copy the code

Enhanced for loop

  • Java5 introduces an enhanced for loop that works primarily with arrays or collections.
  • Grammar:
For (declaration statement: expression){// code sentence}Copy the code
  • Declaration statement: Declares a new local variable whose type must match that of an array or collection element. Its scope is limited to the loop block and its value is equal to the value of the data or collection at that time.
  • Expression: An expression is the name of the array to access, or a method that returns an array.

Break, continue

  • In the body of any loop statement, a break can be used to control the flow of the loop. ==break is used to force out of the loop == without executing the remaining statements in the loop. (The break statement is also used in switch statements)

  • The continue statement is used in the body of a loop. == Terminates a loop == by skipping statements in the body that have not yet been executed and then deciding whether to execute the loop next time.

  • About the goto keyword

    1. The goto keyword has a long history in programming languages. Although goto is still a reserved word in Java, it is not officially used in the language; Java has no GOto. However, we can still see some goto in the break and continue keywords — labeled break and continue.
  • A “label” is an identifier followed by a colon, for example: label:

  • The only place to use tags in Java is before a loop statement. The only reason to set a label before a loop is that we want to nest another loop in it, since the break and continue keywords usually only break the current loop, but when used with labels, they break where the label exists.

  • Exercise: Print triangles

package zch.struct; Public class Test01 {public static void main(String[] args) {for (int I = 1; i <= 5; i++) { for (int j = 5; j >=i ; j--) { System.out.print(" "); } for (int j = 1; j <= i ; j++) { System.out.print("*"); } for (int j = 1; j < i; j++) { System.out.print("*"); } System.out.println(); }}}Copy the code