Well-off will accompany you to learn JAVA -------- While loop of the three major loops, java -------- while

Source: Internet
Author: User

Well-off will accompany you to learn JAVA -------- While loop of the three major loops, java -------- while

The next three blogs will introduce the loop structure that is frequently used in JAVA. This article first introduces the While loop.

While is a loop statement and a condition judgment statement. When you do not know how many times a loop is executed, you need to use the while loop. The while LOOP format is as follows:

1. Before entering the while loop for the first time, you must first assign the starting value to the loop control variable (or expression.

2. Determine whether to continue executing the Loop Based on the content of the judgment condition. If the condition judgment value is True, continue executing the loop subject. The condition judgment value is False ), then, other statements are executed out of the loop.

3. After the statement in the loop body is executed, assign a value (increase or decrease) to the loop control variable (or expression) again, because the while LOOP does not automatically change the loop control variable (or expression) so the designer should assign values to the loop control variables in the while loop, and then return to step 2 to determine whether to continue the loop.

While loop flowchart:

01 // The following program describes how to use the while LOOP

02 public class TestJava3_28

03 {

04 public static void main (String [] args)

05 {

06 int I = 1, sum = 0;

07

08 while (I <= 10)

09 {

10 sum + = I; // accumulate Calculation

11 I ++;

12}

13 System. out. println ("1 + 2 +... + 10 =" + sum); // output the result

14}

15}

Output result:

1 + 2 +... + 10 = 55

Program description:

1. In row 6th, assign the I value of the cyclic control variable to 1.

2. When the first row enters the while loop, the judgment condition is I <= 10. When the first row enters the loop, the value of the judgment condition is true because the value of I is 1, that is, it enters the loop body.

3. 9th ~ 12. The subject of the behavior loop. After sum + I is specified to store the sum, the I value is added with 1 and then return to the starting point of the loop, continue to judge whether the I value is still within the defined range. When I is greater than 10, the loop jumps out, indicating that the accumulative operation has been completed and the sum value is output.

 


Java While LOOP

WHILE (SUM <10), the condition in parentheses is the loop condition, that is, the loop is continued only when SUN <10.
Initial status: sum = 0, I = 1
First time: sum = 0 + 1 = 1; Output I, at this time I = 1; I ++ is I = I + 1 = 2;
After the condition is determined, the loop can continue.
Second time: sum = 1 + 2 = 3; Output I, at this time I = 2; I ++ get I = I + 1 = 3;
After the condition is determined, the loop can continue.
Third time: sum = 3 + 3 = 6; Output I, at this time I = 3; I ++ get I = I + 1 = 4;
After the condition is determined, the loop can continue.
Fourth time: sum = 6 + 4 = 10; Output I, at this time I = 4; I ++ get I = I + 1 = 5;
Sum = 10, and the loop ends.

Java while LOOP

The expression e = I in the outer loop is to assign the variable value to e. The first is e equal to zero, the second is 1, and the third is 2. So on; until 9. E is used to return the number of times in the cycle to e.

E -- is one e variable minus 1 per loop, equivalent to e = E-1; without e -- cannot control how many cycles the loop actually has to be recycled. It is an endless loop. When the ratio of e is 2, the condition 2> 0 is met. Then, the cycle is 1, e minus, and e is 1;
1> 0; meet the condition; one cycle; e --; then e is equal to zero; if the condition is not met, the internal loop is exited and the External Loop is exceeded;

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.