I think you understand the concept of increment and decrement wrong, see, in the first case you are actually decreasing the balance variable by -100, however; in the second case you are assigning the variable the value -100.
See the example below of a decrease:
class Teste {
public static void main(String[] args) {
int saldo = 14000;
int i = 0;
for(i=0; i < 5; i++) {
saldo -= 100; //Aqui tem um decremento
System.out.println("saldo : "+ saldo);
}
}
}
Its result is:
saldo : 13900
saldo : 13800
saldo : 13700
saldo : 13600
saldo : 13500
Now notice if you do the reverse, which is to put the sign - after the =assignment signal, getting sando =- 100
class Teste {
public static void main(String[] args) {
int saldo = 14000;
int i = 0;
for(i=0; i < 5; i++) {
saldo =- 100; //Aqui tem uma atribuição, isto é, mesma coisa que saldo = -100;
System.out.println("saldo : "+ saldo);
}
}
}
Result, was -100 in everything, because each loop the variable balance was assigned the value of -100:
saldo : -100
saldo : -100
saldo : -100
saldo : -100
saldo : -100
Are you sure you’ve seen
saldo =- 100
somewhere? You’re basically assigning -100 to the variable. I don’t see this as possible considering the same context ofsaldo -= 100
.– Woss
Not to mention that the doubt is the same, the answers answer to both cases.
– user28595
@Article thank you!
– Jorge.M
@Andersoncarloswoss, where does the example given interfere with the answer? I believe it did not affect the understanding, so much so that the staff have already given some concrete answers.
– Jorge.M
Yes, that was the question. Anyway, thanks for the comments.
– Jorge.M
reply here https://stackoverflow.com/questions/8710619/why-dont-javas-compound-assignment-operators-require-casting
– Edu Mendonça