All things are lawful, but not all things are profitable.
Roel De Nijs wrote:If you really want some fun, you definitely have to look at this thread. It's all about the quirkiness of the i = i++; statement
Guillermo Ishi wrote:You must keep some kind of index to threads. You always have the previous one or better one at hand.
We have lots of threads about that. That behaviour is exactly in line with the stipulations of the Java® Language Specification.Guillermo Ishi wrote:. . . that we had a thread about or diverted a thread to.
. . .
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Clear code is the best code.
Shawn Lau wrote:I don't even know why books would suggest that. Its bad programming style.
Paul Clapham wrote:
And those exams are packed full of horrible code examples which are supposed to test your knowledge of Java. Those books should have a big disclaimer.
Campbell Ritchie wrote:that behaviour is exactly in line with the stipulations of the Java® Language Specification.
… and that is one of my bigger lies this monthA few minutes ago, I wrote:. . . The behaviour is very simple to understand.
At line 2 the value of i is 124. Butthe value 1 is added to the value of the variable and the sum is stored back into the variable.
That means the value of the whole expression i++ is 123. This is what confuses people, that there are two different values going round. If you use the daft codeThe value of the postfix increment expression is the value of the variable before the new value is stored.
Campbell Ritchie wrote:This behaviour goes back to the days of C in 1972 (and probably before that), but it is not strictly defined in C.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
That copy does have ANSI C written on the cover.Kernighan & Ritchie, in the C Programming Language 2/e (1988) page 46 wrote:… But the expression ++n increments n before its value is used, while n++ increments n after its value has been used. This means that in a context where the value is being used, not just the effect, ++n and n++ are different. If n is 5, then
x = n++;
sets x to 5, but
x = ++n;
sets x to 6. In both cases, n becomes 6. …
Personally, I wish Java had never allowed the damn things (postfix operators) - although oddly, it's the way most of us (including me) code our for loops:
for(int i = 0; i < array.length; i++) { ...
Clear code is the best code.
Campbell Ritchie wrote:Trouble is, the C chappies used to work with small screens and believed it was good practice to write the shortest code possible. I think they believed the computer would overload and burn out if you pushed the space bar more than once per page. That is the explanation for abominations like what you have just demonstrated.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
It's just a flesh wound! Or a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|