Home  >  Article  >  Java  >  Analysis of the execution process of for loop in Java

Analysis of the execution process of for loop in Java

高洛峰
高洛峰Original
2017-01-21 16:04:411854browse

The example of this article analyzes the execution process of for loop in Java. Share it with everyone for your reference. The specific analysis is as follows:

public class Test01{
public static void main(String[] args) {
  int i = 0 ;
  for(foo(&#39;A&#39;);foo(&#39;B&#39;)&&i<3;foo(&#39;C&#39;)){
  i++ ;
  foo(&#39;D&#39;) ;
  }
}
public static boolean foo(char c){
System.out.print(c + " ");
return true ;
}
}

What is the output result of this program?
Yes, it is: A B D C B D C B D C B

Why is this? Because the for loop first executes 'A' before the first semicolon, and then executes 'B', and then executes the code in the for loop if the conditions are met
Then jumps to 'C' after the second semicolon After this execution, compare 'B' to see if it meets the condition. If it does, continue to enter the for loop.
That is, BDC will continue to execute in a loop until the following conditions are not met when running B, and the last 'B' will be output. .

After seeing this, do you have a deep understanding of the for loop?

I hope this article will be helpful to everyone’s Java programming.

For more articles related to analysis of the execution process of for loop in Java, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn