Tuesday, January 24, 2017

Learn Java for Beginner easily ! part 8 : Increment Operator

In programming world, there are stuff called increment. This Increment Operators are increasing the value of a variable , that's because you can change the value of a variabel without changing its first value . But, it is not always about increasing the value. Because with these increment operators, you can also decreasing the value of a variable as how you want it. Maybe in this level of tutorial you are not gonna completely understand it but somehow this is going to be useful when you have reached the advanced level of programming.

Let me show to you guys what I mean by "increasing the value of a variable without changing its first value" on the picture below.





On the picture above, I have declared variable mango as an integer data type with 5 as its value. But when I write the keyword to print out ( System.out.println(mango); ) the value of mango variable, the value of the variable becomes 6. It means the value has increased 1 point.

Q : How is it possible  ?

A : Look at Line 7 on the picture above. I wrote something there that might have pumped your curiosity.

So whenever I type "++" before a variable, it means that I've told java that I want to increase 1 point of a variable that I will write after the "++" sign. On the picture above (Line 7), I told java to increase the point of variable "mango". One more think, you can also write the Increment operators this way :


Q : So, what happened if I type the "++" sign after the name of the variable ?

A : You mean like this ?


A : Yes ! and why is the value not increased ?

Q : So whenever we write a program. The computer will read it from top to bottom and from left to right. In the case of the picture above, the "++" sign is written right after the variable's name. What happened is the computer accept an order to print out the value of "mango" variable first than the order to increase the value of the variable. So that's why the value that printed out is not increased.That's is because the value is still 5 when I told Java to print it out and suddenly It becomes 6 right after the value printed out. Do you want a proof ? Take a look on the picture below :



See ? The value becomes 6, which means It increased when I told Java to print out the value again without adding any increment operators.

Q : Hmm I see, But how if I want to increase the value more than 1 point ? is it able to do that ?

A : Of course, Take a look on the picture below. This is how you increase the value if you want it more than 1 point :


In this case, I increased the value with 3 points. But you can also increased the value as many as you want it.This is called Assignment Operator.You can also make it simplier by doing it this way :

One last thing, like I said in paragraph 1. Increment Operators is not always about increasing the value. It also can be divided,multiplied,and reduced. All you have to do is to change the basic Math Operators sign with the operator sign that you want it to operate with.

So that's it guys. Thanks you for visiting my blog. If you guys have any advice in my explaining. Let me know by leaving a comment below or if you guys have any question to asked. Don't be hesitate to use the comment feature.

Greetings from Programmer !

No comments:

Post a Comment