# While Loops in Java – II

Hello! This tutorial is also about while loops that we have learnt from the previous tutorial.

Let’s check another example of while loop….

Now suppose I want to get the sum of the numbers from 1 to 10. How do I find this using java?

or else; you may make a new variable, assign the sum of those numbers and print it.

Easy 🙂 Isn’t it. But HOW would you get the sum of the numbers from 1 to 10000? Writing this manually would be unbelievably exhausting. Isn’t there any easy way? Again 🙂 Use Java while loop.

Right, here’s the code example of getting the sum of 10000 numbers.

### Step 1 :

Setting the integer variable i and assign the value 1 to it.

### Step 2:

Declaring the integer variable sum which will store the sum of 10000 numbers at the end of the loop.

### Step 3:

We are going to update the value of i continuously by adding 1 to it. Ultimately it will become 10000. Since we want the sum only up to 10000, i should not go beyond 10000. So the <condition> for the while loop is (i<=10000)

Step 4 : the block that will be repeated is written next.

This block will be repeated for 10000 times. See, the number in the variable i is added each time to the sum by the statement sum = sum + i.

Then i is updated by adding 1.

i = i + 1

After adding 1 to i what happens? The computer will again go back to the while statement and check the condition (i<=10000).

In the first cycle, this condition is obviously true. Let’s see…

In the beginning we manually assigned,

i = 1;

and

sum =0;

so from the statement sum = sum + i,

sum becomes 0 + 1

Then, i becomes i+1 from the statement i = i + 1, that is, i = 2.

Going back to the top, now, i =2 so, checking the condition (i<=10000) which is actually (2<=10000) is true as 2 is less than 10000.

Therefore, the block is going to run again.

Now, the second cycle begins.

sum =  1 + 2 = 3 and i = 2 +1 = 3

moving again to the top, (3<=10000) is true and block is running again…….and it goes until i becomes 10001.

At that point (10001<=10000) is false and the block will NOT run again.

Meanwhile sum did its job by adding all i values together.

Step 5 : After the while block, the value of sum is printed.

This is all about how to use a while loop.

Final Note: There is a similar loop called dowhile in java. Its structure is as follows;

do

{

<statements>

}

while (<condition>);

or if a single statement is present no need of brackets.

do

<statement>

while (<condition>);

Here, the <statements> will run first, not considering whether the <condition> is true or false. Then, it will check the <condition> with the while statement at the bottom.

If the condition is true, then the computer will move again to the <statements>.

If the <condition> is false, it’ll just end the loop and progress to the codes below.

The most important thing in a dowhile loop is, it will execute the <statements> first without considering the <condition>. So the statements are going to run at least once.

But in the while loop, we have seen that, it will check the condition first.

The differences between the two loop structures are sum up in the table below…

whiledo-while
Checks the condition firstExecutes the statements first
If the condition is false in the first run, no any statements will run.Statements will run at least once.

In the next tutorial, we’ll learn about another control structure in java called for loops.

Both while loops and for loops are used very often in java programming. So it is important to learn the next loop as well 🙂