Article From:https://www.cnblogs.com/pinlantu/p/9912094.html

In the previous introduction to while loops, an integer variable named year appears frequently, and it is the key element to control the entry and exit of loops. No matter which way of writing, there are three operations related to year, namely “year = 0” and “year & lt”;Imit”,”year++”. The first “year = 0” is used to assign an initial value to the variable, the second “year & lt; limit” is used to determine whether to exit the loop, and the third “year ++” is used to increase the operation of the variable. These three sentences are combinedUp to now, the cycle can be processed in a limited number of times, not in an infinite number of times. In other words, in order to achieve a standard circular structure, we must have the three basic operations mentioned above. So Java designed a new for loop, intended to replace the structured whi with a well-organized for statement.The Le statement.
forThe writing format of the loop is like “for (A; B; C;){/* Here is the inner code of the loop */}”, in which formula A is the initialization statement and executes when it enters the loop for the first time; formula B is the judgment condition of the loop, and when B is established, it continues the cycle and exits the cycle when it does not start.Formula C is a self-increasing or self-decreasing operation of variables, which is executed before the next loop begins. As an example of the wake-up game described earlier, the loop code rewritten with the for statement is as follows:

		System.out.println("It's a long night without intentional sleep.System. out. println ("Please set a wake-up time for him");Scanner scan = new Scanner (System. in); // from the consoleReceive input textThe /* nextLine method means that a line of text is received, ending with the carriage return key */Int limit = scan. nextInt ();Int year;/ / for (A; B; C);The three formulas A, B and C are as follows:Formula A is executed when it enters the loop for the first time.// Formula B is the judgment condition of the cycle. When B is established, it continues to cycle, and when B is not established, it withdraws from the cycle.Formula // C is executed before starting the next loop. Attention, every timeAfter the end of the loop, the sub-C is executed first, and then the sub-B is judged.For (year = 0; year & lt; limit; year + +) {System.out.println ("Past Years:"+year));}System. out. println ("He slept so many years:" +year);

 

As can be seen from the above code, the for loop places all three basic operations on the same line, greatly reducing the number of lines of code. Just three lines for statement, equivalent to the following dozen lines of while loop code:

		year = 0;
		if (year<limit) {
			while (true) {
				System.out.println("Past years: "+year";Year++;If (year< limit) {Continue;} else {Break;}}}

 

However, the cost of simplifying code is lack of flexibility. Because the condition judgment of for statement is executed by default before the beginning of each loop, if you want to judge whether to continue the loop at the specified position inside the loop, you still need to move the judgment condition of formula B into the loop, when the condition judgment of for statement was originally given formula.B can be left empty. So the for loop code after moving the condition judgement becomes the following:

		for (int year=0; ; year++) {
			System.out.println("Past years: "+year";If (year & gt; = limit) {// here to determine if you can jump out of the loopSystem. out. println ("He slept so many years:" +year);Brea; // Jump out of the loop. That is, after jumping to the right curly braces of the for loop} else {Continue; // Continue the next cycle. At this point, execute year++, and then execute the inner statement of the loop.}}

 

Since the original position of Formula B is allowed to remain empty, so long as properly handled, the positions of Formula A and Formula C are also allowed to remain empty. An example of for loop code after three positions are left blank at the same time is as follows:

		int year = 0; // Move Formula A to the front of the whole cycleAll three positions after the {//for statement are left blank.System.out.println ("Years past:"+year);If (year)& gt; = limit) {// Here to determine if you can jump out of the loopSystem. out. println ("He slept so many years:" +year);Brea; // Jump out of the loop. Jump to for cycleAfter the right curly braces} else {Year++; // Move Formula C before ContinueContinue; // Continue the next cycle. At this point, execute year++, and then execute the inner statement of the loop.}}

 

But once all three positions immediately after the for statement are left blank, the for becomes featureless, and the “for (;)” at this time is exactly equivalent to “while (true)”. So, what kind of circular form should be adopted should be determined according to the actual business requirements.Take away.

Leave a Reply

Your email address will not be published. Required fields are marked *