Java, Math Operators Increment (++) and Decrement (--)


In this lesson we will learn two 'handy' things, especially useful to study for and while loops.
These operators simply add and subtract the unit.

Sounds simple, don't?
At the end of the course you will be fan of increment and decrement operators.




Counting

In many Java applications have the need for, or control things:

  • how many things are spelled 
  • how many shots you took in the game
  • how much time has passed
  • how many students were enrolled in a system 
  • etc.

Generally, we count one by one. Imagine, manage and count a registration system with million of subscribers, like SAT?

But you are Java programmer and do not get frightened, because you knows that you will not make it.
You're not stupid, will program the computer to do it ;)

So much of the beads are made using statements (while, for, switch...) of flux control, which you will learn soon and will use forever.
With it, you will make your computer do how many accounts do you wish... 10, 20, thousand, one million ... Google performs trillion per seconds.
You can even do a loop, which is an endless loop and lock your computer. Some call it hacking, I prefer to call study and I will show you how to do it.

Those statements, however, have to be increased. Usually they have a beginning, a condition and an end.
As this end is reached? Through one or more variable that is growing or decreasing.
When it reaches a certain point, the loop will stop.

That's where the increment and decrement operators appears.

Adding and Subtracting variables

The increase means:
a = a + 1

Still remember how to solve it?
Pass the 'a' the other site: a - a = 1
There is: 0 = 1

That is, it is not what you had in mind.
a = a + 1, in Java, moreover, in Programming, is something totally different from conventional mathematics.
We'll see in practice that explain later.

Open your NetBeans, declare an integer, assign a value and print.


Now do:
a = a + 1;
Shortly after 1 to assign 'a', prints, check the result and try to figure out what happened.
Replace by: a = a + 2;
Or a = a + 3



public class increment {
    public static void main(String[] args) {
        int a=1; a = a + 1;
        System.out.println(a);

    }

}


Hackers, people who discover things by themselves (and not criminals or vandals, as peoploe think, mistakenly), do it.
Test, think and discover how things work.

Explanation:
When we make a =
is because we assign a value to 'a', a new value, REGARDLESS of what he had before.
And what value we assign? 'a + 1'!
That is, the 'a' will now be its old value plus 1!

If a = 2, and we do a = a + 3, what are we doing?
We are saying that the 'a' will change, will be its former value (2), plus 3 ! That is, now a = 5
Makes sense, no?

a++
a--

We'll use a lot, but the VERY same increment and decrement unit:
a = a + 1;
a = a - 1;

However, it is getting boring or lazy writing and a = a +1 and a = a - 1 all the time.
Then, they invent shortcuts to it!
a = a + 1 can be represented by a++ or ++a
a = a - 1 can be represented by a-- or --a

There is a difference between a-- and --a, or between ++a and a++.
But that's a topic for another article, for now, stick with a code that shows the use of increment and decrement.
Do not stop trying it.

As the philosopher once said: I think just compiling and viewing.


public class increment {
    public static void main(String[] args) {
        int a=1;
        int b=1;
        System.out.println("Initial value a = " + a);
        System.out.println("Initial value de b = " + b);
        System.out.println("Incrementing: a++");
        a++;
        System.out.println("Decrementing: b--");
        b--;
        System.out.println("Now a = " + a);
        System.out.println("Now b = " + b);
    }

}


Exercise:
Test ++a and --b and see if there is a difference or not.
Format with printf instead of println.

No comments: