This article analyzes the execution process of for loops in Java. Share it for your reference. The specific analysis is as follows:
public class Test01{public static void main(String[] args) { int i = 0 ; for(foo('A');foo('B')&&i<3;foo('C')){ i++ ; foo ('D') ; }}public static boolean foo(char c){System.out.print(c + " ");return true ;}} What is the output result of this program?
That's right, it's: ABDCBDCB
Why is this so? Because the for loop first executes 'A' before the first semicolon, then executes 'B', then meets the conditions and executes the code in the for loop and then jumps to 'C' after the second semicolon. After the execution is completed, compare whether 'B' meets the condition. If it is met, continue to enter the for loop, that is, BDC continues to execute the loop until B is run, and the last 'B' is output until the subsequent conditions are not met.
After seeing this, have you gained a deep understanding of the for loop?
I hope this article will be helpful to everyone's Java programming.