Keyword break and Continue
In addition to using break in a switch statement, you can use a break in a loop to immediately terminate the Loop.
There are three types of loop statements: while loops, Do-while loops, and for Loops.
The whole of a statement that needs to be executed repeatedly in a loop is called the Loop body.
The loop body executes one iteration called a loop.
An infinite loop is an infinite number of times a loop statement is Executed.
In the design cycle, both the loop control structure and the loop body should be considered.
The while loop first checks the loop continuation Condition. if the condition is true, the loop body is executed, and if the condition is false, the loop body ends.
The Do-while loop is similar to the while loop, except that the Do-while loop executes the loop body first and then checks the loop continuation condition to determine whether to continue or Terminate.
Since both the while loop and the Do-while loop contain cyclic continuation conditions, This condition is related to the loop body, and the number of repetitions is determined by the loop body. therefore, while loops and do-while loops are not deterministic in terms of the number of Cycles.
The flag value is a special value that is used to mark the end of the Loop.
The For loop is generally used in the case of the loop body execution times, which is not determined by the loop body.
The For loop consists of three parts. the first part is the initial operation, which is typically used to initialize the control Variable. The second part is the cyclic continuation condition, which determines whether the loop body is executed. the third part is the operation after each iteration, which is often used to adjust the control Variables. typically, the loop control variable is initialized and modified in the control Structure.
The while loop and the for loop are all referred to as the front test loop (pretest loop) because the loop continuation condition is detected before the loop body Executes.
The Do-while loop is called the post-test loop (posttest loop), Because this condition is detected after the loop body is Executed.
You can use the keyword break and continue in the Loop.
The keyword break immediately terminates the most inner loop that contains the Break.
The keyword continue just terminates the current Iteration.
packagewelcome;/** Use nested for loops to print a multiplication table*/ public classmultiplicationtable { public Static voidmain (string[] Args) {System.out.println ("multiplication Table");//Show titleSystem.out.print (" "); for(intj = 1; J < 10; J + +) {System.out.print ("" + j);//show numbers from 1 to 9} System.out.println ("\ n----------------------------------------");//Display Horizontal line for(inti = 1; I < 10; i++) {System.out.print (i+ " | "); for(intj = 1; J < 10; J + +) {System.out.printf ("%4d", I * j);//in the inner loop, as each i,j is taken,.., 9, The inner loop shows the value of the product i*j in each row} System.out.println (); } }}
packagewelcome; public classTestsum { public Static voidmain (string[] Args) {Doublesum = 0; intCount = 0; for(Doublei = 0.01; I <= 1.0; i = i + 0.01) {sum+=i; Count++; System.out.println (count+ " " +i); } System.out.println ("the sum is" +sum); Sum= 0; DoubleCurrentValue = 0.01; for(inti = 0; I < 100; i++) {sum+=currentvalue; CurrentValue+ = 0.01; } System.out.println (sum); }}
packagewelcome; public classTestbreak { public Static voidmain (string[] Args) {intsum = 0; intNumber = 0; while(number < 20){//number++;Sum + + + +number ;//number++; if(sum > 100){ break; }} System.out.println ("the number is" +number ); System.out.println ("the sum is" +sum); }}
packagewelcome; public classTestcontinue { public Static voidmain (string[] Args) {intsum = 0; intNumber = 0; while(number < 20) { number++; if(number = = Ten | | number = = 11) Continue;//when number is 10 or 11 o'clock, The Continue statement is executed, and the other statements in the loop body are no longer executed when continue ends the current Iteration.Sum + = number;//add an integer from 1 to 20 minus 10 and 11 to sum} System.err.println ("the sum is" +sum); }}
Java Learning Notes-loop Summary