Cyclic structure is based on the determination of the condition of the establishment or not, determine the number of execution of the procedure paragraph, and this program paragraph is called the loop body. Second, while loop while is a loop statement, is also a conditional judgment statement. When you do not know beforehand how many times the loop executes, it is necessary to use the while loop. The format of the while loop is as follows:
While (cyclic condition judgment) {
statement 1;
Statement 2;
... ..
statement N;
cyclic condition change;
}
Example one, using while to accumulate operations within 100
PackageLoop; Public classTest1 { Public Static voidMain (string[] args) {//use while to accumulate within 100 intsum=0;//define the variable to hold the accumulated value intI=1;//Defining integer Variables I while(i<=100) {sum+=i; I++; } System.out.println ("Sum of integers within 100:" +sum); }}
Results: 100 or less integer sum: 5050
While is the first to determine whether the condition is set up, if set up into the loop, not set up Skip the loop
Example two, the value of ibid I becomes 101
PackageLoop; Public classTest { Public Static voidMain (string[] args) {//use while to accumulate within 100 intsum=0;//define the variable to hold the accumulated value inti=101;//Defining integer Variables I while(i<=100) {sum+=i; I++; } System.out.println ("Sum of integers within 100:" +sum); }}
Results: 100 or less integer sum: 0
Third, the Do-while cycle Do...while cycle is also used for the number of unknown cycles, while the most different while loop and Do...while loop is to enter the while loop before the while statement will test the true and false conditions, and then decide whether to execute the loop body, and do ... While loop is "do it first", each time the loop body is executed first, and then test the authenticity of the condition, so no matter what the condition of the loop, when using the Do...while loop, at least one loop body, the statement format:
do{
statement 1;
Statement 2;
....
statement N;
cyclic conditions change;
}while (cyclic condition judgment);
Example three, using the do-while for up to 100 cumulative operation
PackageLoop; Public classTest { Public Static voidMain (string[] args) {//use while to accumulate within 100 intsum=0;//define the variable to hold the accumulated value intI=1;//Defining integer Variables I Do{sum+=i; I++; } while(i<=100); System.out.println ("Sum of integers within 100:" +sum); }}
Results: 100 or less integer sum: 5050
For the For loop, for the while and do...while two loops, the operation does not necessarily have to know the number of loops explicitly, and if the developer has explicitly
know the number of cycles, then you can use another loop statement to--for the loop. The format is as follows:
For (assigned initial value; judging condition; assignment increment or decrement) {
statement 1;
....
statement N;
}
Example four, use for to do 100 or less cumulative operation
package loop; public class Test2 { static void main (string[] args) {// int sum=0; // Defines the variable to hold the accumulated value for (int i=1; i<=100;i++ =sum+i; } System.out.println (sum of integers within "100": "+sum"); }}
Results: 100 or less integer sum: 5050
4.1 Loop Nesting example five, for loop printing 99 multiplication table
package loop; public class Test3 { static void main (string[] args) {// for (int i=1;i<=9;i++) { for (int J=1;j<=i;j++ + "*" +i+ "=" + (j*i) + "\ T" ); } System.out.println (); } }}
Results:
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
The break statement breaks statement can force the program to interrupt the loop, and when the program executes to the breaking statement, it leaves the loop, resumes execution of the next statement outside the loop, and if the break statement appears in the inner loop of the nested loop, the break statement only jumps out of the current layer's loop. As an example of a for loop, when there is a break statement in the body of a loop, when the program executes to break, it leaves the loop body and continues to execute the statement of the outer layer of the loop. Example VI, implementation of output 1-10, encountered 4 o'clock using break;
PackageLoop; Public classtest4 { Public Static voidMain (string[] args) {//implementation Output 1-10, encountered 4 o'clock program exit for(inti=1;i<=10;i++){ if(i==4){ Break; } System.out.print (I+" "); } System.out.println ("Loop End"); }}
Results: 1 2 3 cycle end
The Continue statement continue statement can force the program to jump to the beginning of the loop, and when the program runs to the continue statement, it stops running the rest of the loop body, but goes back to the beginning of the loop to continue running. As an example of a for loop, there is a continue statement in the body of the loop, and when the program executes to continue, it goes back to the start of the loop and proceeds to the partial statement of the loop body. Continue is skipping the current loop into the next loop example seven, the same as the implementation of output 1-10, encountered 4 o'clock using continue;
PackageLoop; Public classtest4 { Public Static voidMain (string[] args) {//implement output 1-10, encounter 4 o'clock using continue for(inti=1;i<=10;i++){ if(i==4){ Continue; } System.out.print (I+" "); } System.out.println ("Loop End"); }}
Results: 1 2 3 5 6 7 8 9 10 End of cycle
Seven, return statement
Ends the execution of the current method and exits, returning the statement at which the method was called.
Example in, the same as the implementation of output 1-10, encountered 4 o'clock using return;
PackageLoop; Public classtest4 { Public Static voidMain (string[] args) {//implement output 1-10, run 4 o'clock with return for(inti=1;i<=10;i++){ if(i==4){ return; } System.out.print (I+" "); } System.out.println ("Loop End"); }}
Results: 1 2 3
You can see that the "Loop end" has no output, and when i=4, it satisfies the execution return and ends the execution of the entire method.
Java learns eight from zero (circular structure)