Detailed Explanation of Java for Cycle

  • 2021-07-07 07:39:21
  • OfStack

For the for loop in java, what we use most is nothing more than the following statement:


 for (int i = 0; i < 10; i++) {
  System.err.println(i);
}

However, for for loop, it also has many variants. If you don't clearly understand the structural principle of for loop, you may not understand its meaning when you meet one variant of for loop. Although there are many variants of for loop, they can't be separated from the mode of three statements in brackets (except for enhancing for loop mode, which will be discussed later)

The structure of the for loop:


for( Statement A;  Statement B;  Statement C){

   // Circulatory body 

}

Among them, the statement A will only be executed once in the whole loop process; The statement B must be a Boolean expression (of course, it can not be written, if it is written, it must be a Boolean expression), and whether to continue to execute the loop body is judged by this Boolean expression; The statement C executes at the end of each loop, that is, the statement C executes as many times as the body of the loop executes.

The order in which the statements are executed is:

Statement A → (Statement B → Loop Body → Statement C) → (Statement B → Loop Body → Statement C) →... → Exit Loop

Here is a brief introduction to one variant of the for cycle:

Variant 1: Statement enrichment and diversification, as long as the statements A and C are all one statement, and the statement B is a Boolean expression. How to write the statement is up to you.


for (int i = 0, j = 0; i < 10 | j< 12; i++, j++) {
  System.err.println(i);
  System.err.println(j);
  System.err.println("=========");
}

Variant 2: Statement A omitted


 boolean flag = true;
int m = 0;
for (;flag; m++){
  if (m==10)
    flag=false;
  System.err.println(m);
}

Variant 3: The statement B is omitted. At this time, only the loop is exited from the loop body, otherwise it is an infinite loop.


 int m = 0;
for (;; m++){
  if (m==10)
    break;
  System.err.println(m);
}

Variant 4: Statement C omitted


 int m = 0;
for (;;){
  if (m==10)
    break;
  System.err.println(m);
  m++;
}

Note: The above variants are only relative to the commonly used forms. The structural essence of the variants is unchanged, so we don't need to deliberately use the variant forms. The reason why I put it forward is that I just hope that I can understand the meaning of loop as soon as possible when I encounter variants in the future, so as not to see the code written by others, which is different from the way I write it myself, and then I will be covered with a circle. Not all of the variants are listed above; Simply put, the statements A, B, and C can be omitted separately.

For enhancing for loop, I don't introduce it much, and I use it more. The basic structure is:


 Integer[] arr = new Integer[]{1,2,3,4};
for (Integer a : arr){
  System.err.println(a);
}
 
List<String> list = new ArrayList<>();
//add str...
for (String str : list){
  System.err.println(str);
}

Related articles: