Java, Assignment Operators: +=, -=, *=, /= and %=


We saw in Chapter operators step increment (+ +) and decrease (-), which are nothing more than shortcuts for tasks such as:
a = a + 1
a = a - 1

However, to avoid writing 'for nothing', were not only created these shortcuts and not only are these types of assignments.



Assignments


If you already know how works: a = a + 1
Have you stopped to think if there is and how the work: a = a * a?

Come on, be a = 2
How much would be: a = a * a?
Logically, the 'a' would receive the 'a' old value of their former values, 2 * 2 = 4. Correct

Have you studied GP, geometric progression?
This would be a fine example where we would use this assignment. Note that the figure was 'a' is now 'a ²'
And your teacher, who used to put you to calculate GP, GA ... pretty soon you'll program the computer to do it for you.
We will use assignment to make an application that calculates simple interest too.

I have one that, though simple, is quite useful. He asks how much I'm going to put in the bank, the interest rate (tax), months I'll leave the money there, and it tells me what will earn.
Or I say how much income I want and it tells me the months I should leave it there, so I can get the income I want.

Knowing programming, will you do the things you need. That was my example.
What's yours? What you need to calculate every day?
On what account you lose time doing, asking for someone or researching?
At the end of Flux Control section you will be able to do a wide range of applications in Java for its purpose.


Shortcuts that save lives

You've already learned that it is better to write ++a instead of a = a + 1
What about the other assignments? You did not think we would lose that much time writing it?
Of course, no.
Look other tricks in Java:

a = a +  b, do: a += b
a = a  -  b, do: a -= b
a = a *  b, do: a *= b
a = a /   b, do: a /= b
a = a % b, do: a %= b

The logic is simple. You will always see two numbers, x and y, for example.
And a mathematical operator, we call []
And will see the equal sign, =, which is the assignment sign, in Java.

And will see the formula: x []= y
This will always represent this: x = x [] y

That is, the first number will always receive the mathematical operation [] of its old value with the number y.

And not to lose the habit, a Java code to show how it's done:


public class Assignment {
    public static void main(String[] args) {
        int a=1;
        int b=2;
        System.out.println("Initial value a = " + a);
        System.out.println("Initial value b = " + b);
        System.out.println("Doing a +=b");
        a +=b;
        System.out.println("Now a = " + a);
        System.out.println();
        
        System.out.println("Doing a -=b");
        a -=b;
        System.out.println("Now a = " + a);
        System.out.println("Doing a *=b");
        a *=b;
        System.out.println("Now a = " + a);
        System.out.println("Doing a +=2 ");
        a +=2;
        System.out.println("Now a = " + a);
        System.out.println();
        
        System.out.println("Doing a /=b");
        a /=b;
        System.out.println("Now a = " + a);
        System.out.println();
    }

}

No comments: