--a
is similar to
a = a - 1;
which means at first it calculates value of a-1 and then with a = ... it assigns that value back to a.
But in case of -a you are just calculating negative value, but it doesn't doesn't reassign it back to a. So since you are not doing anything with that calculated value it will be lost so compiler informs you that your code doesn't do what you may think it does.
Try explicitly assign that result back to a with
a = -a;
After this instruction a will hold new value which you can use anywhere.
This problem disappears when you are using
System.out.println("Your number is: " + (-a) );
since now compiler sees that calculated value -a is being used (as part of value passed to println method).
--a
is similar to
a = a - 1;
which means at first it calculates value of a-1 and then with a = ... it assigns that value back to a.
But in case of -a you are just calculating negative value, but it doesn't doesn't reassign it back to a. So since you are not doing anything with that calculated value it will be lost so compiler informs you that your code doesn't do what you may think it does.
Try explicitly assign that result back to a with
a = -a;
After this instruction a will hold new value which you can use anywhere.
This problem disappears when you are using
System.out.println("Your number is: " + (-a) );
since now compiler sees that calculated value -a is being used (as part of value passed to println method).
Because you didn't assign the result of the unary minus. The pre-decrement includes an assignment.
a = -a; // <-- like this.
In the second use (printing), you are using the value in the print routine (and not updating a).
Pre- and Post-decrement example understanding
Confusing expression regarding increments and decrements (Java)
java - Do increment and decrement operators decrease readability? - Software Engineering Stack Exchange
Java Objects Increment/Decrement
Videos
Hello All!
I have the following code snipet, which has a result of 2. Can anybody help me why is that? My face is just melting because of this.
int i = 0;
i = i-- - --i;
System.out.println("Result: " + i);Thank you!
Edit: Guys thank you so much for your explanations!
One thing to note that I have found this syntax in an interview preparing material and I would never use something like this.
So during our computer exams based on Java, I was asked to predict the output of the following:
int y = 8; y += ++y + y-- + --y; System.out.println(y);
I thought the answer was 32. ( 7 += 9 + 9 + 7). I later tried this on my computer, and the output was 33. I'm confused. How was this evaluated?
Your question
Yes it's completely ok to use i++ and --j alone in a statement. Most mainstream languages have adopted them.
Yes it's ok to use i++ and --j in more complex expressions, when the precedence is self-explanatory :
- either because it's isolated enough as in
a[i++]or in--j > 0, - or because it's used according to a common language idiom like
*p++(experienced C/C++ programmer know that increment applies to the pointer/iterator and not to the dereferenced value).
Additional advise
Avoid in case of doubts! Prefer readability and simpler steps instead of using parentheses as for **p[i]++. Abuse like --(*p++) or 1---j should really be forbidden, not for syntactic ambiguity, but to the huge effort it requires to understand.
Absolute prohibition: Never use more than one such side effect on the same variable in the same expression as in *p++ == *--p or funct(i++, i--) . This might lead at worst to undefined behavior, at best to misunderstandings as there are subtle differences across languages.
Conclusion and quote of the day:
" Debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it ? "
B.W.Kernighan
Let's walk through some logic:
- Some junior developers don't fully understand these statements
- So we avoid using them and limit their exposure to such statements
- Eventually all developer will encounter such a statement
- They misunderstand the statement because of #2
In a nutshell, when statements are avoided because they are considered esoteric they become more esoteric. The problem is that if these things are part of the language, they will be found in code somewhere. You might be able to prevent their use on a team but in a way you creating ignorance in doing so.
This problem is why I am not a big fan of "everything plus the kitchen sink" languages. The more syntax there is, the more there is to understand and if some of it is kind of cruddy or awkward, it becomes esoteric and eventually causes a bug somewhere when it is misunderstood. Adopting such a language and then declaring that people shouldn't use the syntax it offers isn't really optimal, in my opinion. Python doesn't allow ++i and i++ and if you hate these statements, that's an advantage of that language.
In languages that allow these things, my answer is "know your tools". These statements are well defined, quite logical, and are useful in common situations. If you are programming in a language that allows these statements and don't know what they mean, you need to skill-up and it's not the responsibility of other developers to coddle you.