For loop
The number of times the For loop executes is determined before execution. The syntax format is as follows:
for (initialize; Boolean expression; update) {// code statement }
There are several explanations for the For loop:
- The initialization step is performed first. You can declare a type, but you can initialize one or more loop control variables, or you can be an empty statement.
- Then, the value of the Boolean expression is detected. If true, the loop body is executed. If False, the loop terminates, starting execution of the statement following the loop body.
- After the loop is executed, the loop control variable is updated.
- Re-detects the Boolean expression. The loop executes the above procedure.
Java Enhanced for loop
JAVA5 introduces an enhanced for loop that is primarily used for arrays.
The Java enhanced for loop syntax format is as follows:
for (Declaration statement: expression) {// code sentence }
Statement statement: declares a new local variable that must have a type that matches the type of the array element. Its scope is scoped to the Loop statement block, whose value is equal to the value of the array element at this time .
expression: The expression is the name of the array to access, or is the method that returns the value to the group.
Public classTest { Public Static voidMain (String args[]) {int[] numbers = {10, 20, 30, 40, 50}; for(int x:numbers ) {System.out.print (x); System.out.print (",");} System.out.print ("\ n"); String [] Names={"James", "Larry", "Tom", "Lacy"}; for( String name:names ) {System.out.print (name); System.out.print (",");}}}
Continue keywords
The Continue is suitable for any loop control structure. The function is to let the program jump immediately to the next iteration of the loop .
In the For loop, the continue statement causes the program to jump immediately to the UPDATE statement.
In the while or Do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.
Java for Loop