In the previous chapter or article in the series of getting familiar with Java we learn about “for” loop and in it we learn how to put increment and decrement in right order and how right the initial value, then for condition and then increment or decrement to produce series of integer or number. Here, we will be doing “while” loop. First open Eclipse and then FILE and then FILE and then NEW and then CLASS to open the NEW JAVA CLASS sub window.

As we have been doing this before, in the NAME write down class name as “ExampleWhile” and then click on WHICH METHOD STUBS WOULD YOU LIKE TO CREATER and there put the tick mark on PUBLIC STATIC VOID MAIN and then click on FINISH. Now within secods through auto generated method stub you will have “ExampleWhile.java” inside src folder of PACKAGE EXPLORER. This would be like as follows:

package day1.examples; public class ExampleWhile { public static void main(String[] args) { // TODO Auto-generated method stub } } “while” loop: int x = -10; while ( x<= 0) System.out.println("x =" +x); x++;

Now let us discuss about this code. While integer x is equal to -10 the first statement, and then it checks the first while condition whether x which is -10 is less than or equal to 0 and it satisfies and then it print x is -10 as the first number and then it goes to increment which is x++ and which is nothing but the x+1 and -10 plus 1 is -9 and it then checks the value of -9 in while condition whether it is true or not and then it finds that -9 is less than or equal to 0 and then it prints, and then, again -9 plus 1 is -8 and x value is now -8, and then it comes to while statement to check that whether -8 is less than or equal to 0 and it satisfies and then it prints out as -8 and then +x means printing out the same number again.

**So ours new code will be like as below.**

package day1.examples; public class ExampleWhile { public static void main(String[] args) { // TODO Auto-generated method stub int x = -10; while(x <= 0){ System.out.println("x = " +x); x++; } } }

Then right click anywhere at the code explorer window and then click as RUN AS and then, JAVA APPLICATION and in the console window the results will be as follows:

x = -10 x = -9 x = -8 x = -7 x = -6 x = -5 x = -4 x = -3 x = -2 x = -1 x = 0

So, the while loop goes on until and unless the value of x reaches to 0. Then when the values after 0 increment to 0 plus 1 which is equal to 1 and then en it reaches to while condition which is the value of x should be equal or less than 0 but it is now 1 which means this is the end of while loop and this values of x which is equal to 1 will not be printed into console.

So, in short the while loop of the code is we have initial condition of integer x is equal to -10 and the while condition is that it should be less than or equal to 0 then print it and then after recheck with increment and this means the value to be increased by 1 and then the entire process continues until and unless the value of integer x is equal or less than 0.

**If you are confused with increment function such as x++ then you can simply write it as :**

x = x + 1;

This is similar to x++ an you can run as and find it on the console. So, ours new code will be like as below.

package day1.examples; public class ExampleWhile { public static void main(String[] args) { // TODO Auto-generated method stub int x = -10; while(x <= 0){ System.out.println("x = " +x); //x++; x = x + 1; } } }

Here, the code is same as below and instead of increment sign such as x++ we prefix the comment // and then we write down the simpler part of increment such as:

X = x + 1; This is the statement so semicolon at the end of this statement and then right click anywhere at the code window and then RUN AS an then RUN AS JAVA APPLICATION to find out the same set up code as previous and this proves that the increment of x which is x++ is similar to this code of x = x + 1;

x = -10 x = -9 x = -8 x = -7 x = -6 x = -5 x = -4 x = -3 x = -2 x = -1 x = 0

In both of these cases the output window produces the similar results. The third way of writing the incrementing value is as follows. Instead of first two examples of incrementing values we can write down the third one which is as follows.

**X += 1**;

This is exactly the same as x++ and x = x + 1 and all of these are of the same value and you can use any of these for incrementing values of x or declared x integer. So, ours new code is something like below.

package day1.examples; public class ExampleWhile { public static void main(String[] args) { // TODO Auto-generated method stub int x = -10; while(x <= 0){ System.out.println("x = " +x); //x++; //x = x + 1; x += 1; } } }

For the sake of keeping the code clean we just put comment prefix at earlier two incrementing examples. Now, again right click on the code and then RUN AS and then RUN AS JAVA APPLICATION to fin out that at the console you should be finding the same results as that of earlier cases and this justifies that all of these three incremental variances at work.

x = -10 x = -9 x = -8 x = -7 x = -6 x = -5 x = -4 x = -3 x = -2 x = -1 x = 0

## Arithmetic operators:

Here in the stage of increment we do use, x += 1 which is also known as addition assignment and we can have several such arithmetic operator which can be used as assignment such as x -= 1 which means x is equal to minus 1, this is an example of substation assignment and now we can have multiplication assignment such as x *= 1 which is x is equal to x into 1. It is an example of multiplication assignment. Similarly the example of division assignment is x /= 1 which is x is equal to x by 1.

Similarly in the case of modulus assignment which is the remained after division, we can write it as x %= 1 which is x is equal to remainder of x by 1. In the earlier examples we do study in depth about addition + then subtraction – and then multiplication * and then division / and then modulus % and then increment ++ and then decrement – and now different assignment are all of these grand part of arithmetic operator.

In this article we study in depth about while loop, increment and arithmetic operators.