
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?
1 |
System.out.print(1+2+3+4+5+6+7+8+9+10); |
or else; you may make a new variable, assign the sum of those numbers and print it.
1 2 |
int sum = 1+2+3+4+5+6+7+8+9+10; System.out.print(sum); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class WhileLoops { public static void main(String[] args){ int i = 1; int sum = 0; while (i<=10000) { sum = sum + i; i = i + 1; } System.out.println(sum); } } |
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.
1 2 3 4 |
{ sum = sum + i; i = i + 1; } |
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 do–while 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 do–while 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…
while | do-while |
---|---|
Checks the condition first | Executes 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 🙂