"While Loop statement
While loop instance diagram:
Not much to say, the first example:
Package cn.bdqn; Public class Test { publicstaticvoid main (string[] args) { int Num=1; // cycle conditions num<= while (num<=10) { System.out.println ("+num+"); // Iteration Statements num++; }}}
Results:
It can be seen from above that the loop condition is evaluated before each loop of the while loop, and the loop body portion is run if the loop condition is true. The iteration condition is always at the end of the loop body, so the iteration statement can only be executed if the loop fitness completes successfully.
When using the while loop, make sure that the loop condition becomes false, and that the loop becomes a dead loop.
Example of a dead loop:
Packagecn.bdqn; Public classTest { Public Static voidMain (string[] args) {intNum=1; //Cycle conditions num<= while(num<=10) {System.out.println ("First" +num+ "Write Code"); //Iteration Statements//num++; } }}
Results:
In this code, the value of num always equals the 1,num<=10 loop condition is always true, which causes the loop condition to never end and becomes a dead loop.
"Do and Loop statement
The difference between a do and while loop is that while the loop condition is first judged, if the condition is true then the loop body is executed, and the Do While loop executes the loop body first, the next loop is executed if the condition is true, or the loop is terminated.
Do While loop instance diagram:
Instance:
Public class Test { publicstaticvoid main (string[] args) { int x= Ten; Do { System.out.println ("Value of x:" + x); X+ +; } while (x<20);} }
Unlike the while loop, the loop condition of the Do While loop must have a semicolon that indicates the end of the loop.
Run code results:
The value of the loop condition starts with a false, and the Do While loop also executes the loop body. Therefore, the loop body of the Do While loop is executed at least once.
The following code will verify this conclusion:
Public class Test { publicstaticvoid main (string[] args) { int x= ; Do { System.out.println ("Value of x:" + x); X+ +; } while (x<20);} }
Results:
"For Loop
The For loop is a more concise looping statement, and for the most part, the for loop can be used instead of the while loop. The basic statement format for the For loop is as follows:
There are several explanations for the For loop:
"Is the first to execute an expression 1. You can declare a type, but you can initialize one or more loop control variables, or you can be an empty statement.
"And then check the value of expression 2. 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.
"Executes expression 2 again, looping through the above procedure.
Instance
Public class Test { publicstaticvoid main (string[] args) { for ( int i = 0; I < 10; i++) { System.out.println ("Value of x:" + i);}} }
The above code executes the result:
Several problems that are prone to arise:
Public Static void Main (string[] args) { /*for (int i = 0;; i++) {//Omit condition judgment, trapped in dead loop System.out.println (i ); } */ /* for (;;) { //Dead Loop System.out.println (1); } */ for (int// omitted conditional-judging iteration variable System.out.println (i);} }
java--Cycle Structure