Increment while now. Also, a few compound assignment operators. It's also a story that you should do it at the time of four arithmetic operations.
There is an increment operator (++) By adding "++" before and after a variable, you can add "1" to that variable.
"++ a" and "a ++" are almost the same as "a = a + 1"
But I was told that the two movements are slightly different. I couldn't quite understand the meaning.
Maybe it wasn't because I saw "++ a" or "a ++" alone.
By using a variable other than the variable to increment You may have noticed something that made a difference.
For this ↓, first do "a = a + 1" and put the result in "b".
b = ++a;
If you imagine the calculation order, it feels like "b = (a = a + 1)"? On the other hand, ↓ This is "a = a + 1" after entering the value of "a" in "b".
b = a++;
If you imagine the calculation order, it feels like "a = a + 1 from b = a"?
I decided to run it for the time being and see the difference in the results. First of all, if you put "++ a" in the usual uninflected word.
int a, b;
a = 1;
b = ++a;
System.out.println("a = " + a);
System.out.println("b = " + b);
・ Because "a = a + 1" is done first, "a = 2" ・ Because "b = a", "2" is entered, so "b = 2"
Execution result Right.
Next is when "a ++" is inserted
int a, b;
a = 1;
b = a++;
System.out.println("a = " + a);
System.out.println("b = " + b);
・ Because "b = a" is done first, "b = 1" ・ Next, "a = a + 1", so "a = 2"
Execution result I see, i see.
Depending on the situation, "++ a" and "a ++" may be used properly.
By the way. .. .. The decrement is subtracted by "1", and the operator is "-".
Similar to increment, but with the formula omitted? But I think.
You can also write "a = a + 5" as "a + = 5".
This "+ =" seems to be a compound assignment operator.
・ Be careful in order "+ =" instead of "= +" ・ There are also "+ =", "-=", "* =", "/ =", etc. ・ There are ">> =" and ">>> =", but I forget them now, and I can't remember them all at once.
I will check the operation when I somehow keep in mind. Add ↓ to the usual basic form and execute.
Yomogi.java
int a = 1, b = 1;
a += 5;
b = b + 5;
System.out.println("a = " + a);
System.out.println("b = " + b);
Confirm that the same result is obtained with "+ =".
Run
Until you can afford it, the main usage is likely to be "a = a + 5".
This time up to here.
Recommended Posts