Java basics: 12th circular structure

Source: Internet
Author: User

 

Loop StructureThe function of completing the loop structure is to execute a code segment cyclically multiple times. The loop structure can be a for loop, for-each loop, while loop, Or do-while loop. 1. For LoopThe basic structure of the For Loop is as follows: for (expression 1; expression 2; expression 3) {// loop body} expression 1 is used for initialization and is executed only once throughout the loop process; the result of expression 2 should be a logical value to determine whether to loop. If expression 2 is true, the loop continues. If expression 2 is false, the loop ends. Expression 3 is executed after each loop is completed, the main function is to modify the loop variable, and the number of cycles will be executed. The loop body is the part to be executed cyclically. If the loop body contains only one line of code, the braces of the loop body can be omitted. The specific execution process is as follows: (1) first execute expression 1 for initialization; (2) then execute expression 2. if the result is true, execute step (3, if the result is false, execute step (5); (3) execute the loop body; (4) execute expression 3, turn to expression 2; (5) end from this process, we can see that, expression 2à loop à expression 3 forms a loop. Expression 1 completes initialization only before the loop, and expression 2 determines whether to loop. The following is an example. For example, calculate the sum of the 100 numbers from 1 to 100. Analysis: if artificial computing can do this, 1 + 2 + 3 + 4 + ...... Of course this can also be done by computers, but what if the sum of the 10000 numbers from 1 to 10000 is calculated? It is too tired to write. The sum of the numbers 1 to 100 can be understood as follows: the sum of the numbers 1 to is 0 at the beginning, and 1 is added to the sum for the first time, add 2 to "and" for the second time, and add 3 to "and" for the third time ...... After 100, the sum is obtained. It is equivalent to adding a number to "and" each time and repeating the number for 100 times. The difference is that the value added each time is different. In this way, you can set a variable, then, after each calculation, modify the value of this variable. If this variable is I, We can first let I be equal to 1. After execution, let I be equal to 2 ...... In this way, you can use the for loop to complete the process. First, define a "and". Here sum is used, and the initial value is 0. You can write it as follows: int sum = 0; then the variable I is defined, and int I is used for each loop; at the beginning, I was equal to 1. This can be done through expression 1 in the loop structure. Expression 1 completes the initialization task, so expression 1 can be written as: I = 1; the task to be completed in the loop is to add I to the sum, so the loop body should write: Sum = sum + I; after each loop, you need to change the I value, how can it be changed? From 1 to, 1 is used up; 2 is used up; 3 is used up; 1 is added on the basis of the original; The I value must be changed after each loop, so expression 3 can be used, expression 3 is executed after the loop is completed. So expression 3 can be written as: I ++; the next question is, when is the loop? To calculate the sum between 1 and 100, When I <= 100, you need to add I to the "and". If I> 100, you do not need to recycle it, so the condition of the loop is I <= 100, expression 2 is used to control whether the loop continues, so the content of expression 2 can be written: I <= 100 the loop structure has several parts, so the following code is obtained: [Example] // fortest. javapublic class fortest {public static void main (string [] ARGs) {// sum storage and INT sum = 0; // I indicates the cyclic variable int I; // I = 0 to initialize the loop variable. I <= 100 indicates the loop condition. // I ++ modifies the value of the loop variable for (I = 0; I <= 100; I ++) {// cyclic body sum + = I;} system. out. println ("sum:" + sum) ;}} the running result is: sum: 5050 when using the for loop, the following points must be clarified: (1) statements to be executed cyclically, that is, the body of the loop; (2) what is the initial state of the loop, that is, the content of expression 1; (3) where is the difference between each loop, and how to modify the changed content, that is, the determination of the content of expression 3; (4) determine the condition of the loop, and when the loop ends, that is, the content of expression 2. The above is the most general case, and some special cases may occur below: (1) expression 1 is used for initialization and only once, so it can be considered irrelevant to the loop, the initialization can be completed before the loop, which forms the following structure: expression 1 for (; expression 2; expression 3) the above sum code can be changed to the following code (part of the main method): int sum = 0; int I; I = 0; // expression 1 is empty, but the semicolon cannot be omitted for (; I <= 100; I ++) sum + = I; system. out. println ("sum:" + sum); (2) after each loop, use expression 3 to modify the value of the loop variable. If it is repeated once, expression 3 is executed once, therefore, expression 3 can be placed in the loop body, and the effect is identical. Therefore, the following format is available: For (expression 1; expression 2 ;) {// loop body expression 3} can be changed to the following code: int sum = 0; int I; (I = 0; I <= 100;) {sum + = I; I ++;} system. out. println ("sum:" + sum); (3) expression 2 can also be omitted. If it is omitted, the loop will have no conditions and the loop will not end here, the value equivalent to expression 2 is true. So how can we stop the loop? You can end the loop in the loop body and use the break that will be discussed later. You can change the for loop to the following format: For (expression 1; expression 3) {If (! Expression 2) break; // loop body} [note] Because expression 2 is a loop condition, but now you need to end the loop, you need to reverse expression 2. According to this structure, the above Code can be changed to: int sum = 0; int I; for (I = 0; I ++) {If (! (I <= 100) break; sum + = I;} system. out. println ("sum:" + sum); (4) the most typical case is that all three expressions are omitted to form the following structure: expression 1 for (;) {If (! (Expression 2) break; // loop body expression 3} the above Code is changed to: int sum = 0; int I; I = 0; (;;) {sum + = I; I ++; if (I> 100) break;} system. out. println ("sum:" + sum); [note] (1) no matter how it changes, the semicolons used to separate the three parts in the for loop cannot be fewer. (2) You cannot add points after the for () Brackets. If you add them, the loop body is an empty statement. The following are eight forms of for loop: N for (expression 1; expression 2; expression 3 ){...} N for (; expression 2; expression 3 ){...} N for (expression 1; expression 3 ){...} N for (expression 1; expression 2 ;){...} N for (; expression 3 ){...} N for (; expression 2 ;){...} N for (expression 1 ;;){...} N (;;){...} 2. While LoopThe while loop function is basically the same as the for loop, but the structure is different. The basic structure is as follows: While (expression 1) {// loop body}. This structure is basically the same as that after the initialization part is omitted in the for loop body and the loop state part is modified, expression 1 is the condition of a loop, which is the same as expression 2 in A for loop. This structure is easier to understand. If expression 1 is set to true, the loop body is executed. Otherwise, the loop ends. The value of expression 1 changes with the execution of the loop body. Otherwise, it is either an endless loop or not executed once. So the loop body here is equivalent to the loop body in the for loop and the part that modifies the loop state. For example, if the sum of 1 to n is close to 10000, evaluate N. Analysis: The sum between 1 and 2 is the sum of 3, 1 to 3, and the sum between 6 and 4 is 10. As N increases, it is closer to 10000, but when the sum is close to 10000, if it is increased, it will be far away from 10000. Therefore, the condition for the end of the loop is that the distance between this and 10000 is the minimum. If the distance does not reach the minimum, the loop continues. First, you need to define the sum, define the difference value, define the cyclic variable, and initialize the variable: int sum = 0; int dis1 = 10000; indicates the new gap int dis2 = 10000; int I = 1; then, specify the loop content: Sum = sum + I; dis1 = dis2; dis2 = 10000-sum; if (dis2 <0) dis2 = sum-10000; I ++; then define the condition for loop end: dis2 <= dis1 Based on the above analysis, get the following code: [Example] // whiletest. javapublic class whiletest {public static void main (string [] ARGs) {// represents and INT sum = 0; // represents the difference between the last time and the 10000 int dis1 = 10000; // The difference between the current time and the 10000 int dis 2 = 10000; // The cyclic variable int I = 1; while (dis1> = dis2) {sum = sum + I; dis1 = dis2; dis2 = 10000-sum; if (dis2 <0) dis2 = sum-10000; I ++;} I = I-2; system. out. the result of println (I) ;}} is: 141 [note] No extra points can be added after the parentheses OF THE while loop. 3. Do-while loopSimilar to the while loop, the format is as follows: do {// loop body} while (expression 1); expression 1 is still a loop condition, which is basically the same as the while loop, there are two differences: (1) first loop, then judge the condition, so at least one loop can be performed; (2) while (expression 1) must be followed by a semicolon. [Example] output an even number between 1 and 20. // Dowhiletest. javapublic class dowhiletest {public static void main (string [] ARGs) {int I = 1; do {if (I % 2 = 0) system. out. println (I + ""); I ++;} while (I <= 20) ;}} the while loop is different from the do-while loop only when the first loop is used, the subsequent processes are exactly the same. If you can execute them once, the two will have the same effect. The main difference between a for loop and a while loop is that a for loop is usually used when you know the number of cycles, while a while loop is usually used when you do not know the number of cycles. Last Lecture: 11th lecture selection Structure Next Lecture: 13th talk about basic syntax exercises (see if you will make any mistakes)Li xucheng csdn blog: http://blog.csdn.net/javaeeteacher invites you as a friend: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.