For Loop format:
For (initialization expression; loop condition expression; Operation expression after loop ){
Execution statement
}
Public ClassFordemo {Public Static VoidMain (string [] ARGs ){//Int x = 3; space is opened in the memory only when the program reads it, and the memory space is released as long as the for loop ends.//The first must be a valid expression, and the second must be a conditional expression. For(IntX = 3; x <8; X ++) {System. Out. println ("X =" +X );//X is valid only in the brackets.}//System. Out. println ("x =" X );//Compilation error. The variable X cannot be found. IntY = 3;While(Y <8) {System. Out. println ("Y =" +Y);} system. Out. println ("Y =" + y );//The result prints the value of Y. /*1. the variable has its own scope. For example, if the incremental definition used to control the loop is in the for statement, the variable is only valid for statement execution in the for statement, this variable is released in the memory. 2. for and while can be exchanged, which can be used, but if you need to define loop increments, use for to save memory*/}}
Common problems with for loop usage
For(System. Out. println ("A"); system. Out. println ("B"); system. Out. println ("C") {System. Out. println ("D");}//No result. The conditional expression is neither true nor false, so no result is returned.
Public ClassFortest1 {Public Static VoidMain (string [] ARGs ){IntX = 1;For(System. Out. println ("A"); x <3; system. Out. println ("C") {System. Out. println ("D"); X++;//It can also be placed behind the for brackets and separated by commas.}//The result is: A D C//For (multiple expressions can be separated by commas)}}
The simplest form of infinite Loops
For (;) {} // true if the for statement condition expression is left blank
While (true ){}
Process control statement _