3 ms·
Slightly to the side of the topic, but this quote here: >Yep, Bob uses i++ instead of ++i everywhere. Meh, what’s the difference. I know what the difference i
by jedimastert 8y ago
Slightly to the side of the topic, but this quote here:
>Yep, Bob uses i++ instead of ++i everywhere. Meh, what’s the difference.
I know what the difference is, but if you're just using it on its own line or in a for loop, why would you use `++i` instead of `i++`?
- saagarjha 8y agoHabit?
- eMSF 8y agoWhy didn't you ask the question the other way around? :-) For some inexplicable reason the postfix operators are usually taught and learnt first, and thus considered more natural, even though there are more objective reasons for the opposite, at least in C++ world.
- derefr 8y ago> For some inexplicable reason the postfix operators are usually taught and learnt first, and thus considered more natural The (unary) postfix operators might have semantics that are more surprising than the prefix variants, but the postfix syntax itself feels a bit more natural and fitting with other syntax of the Algol language family, IMHO, when compared to the prefix syntax. I think that's what leads people to introduce it first. You can sort of read `i++` as an "abbreviation" of `i += 1`—in both cases, you've got an operator that follows an lvalue variable, and which both reads from and mutates the memory referred to by that variable. The only difference is that one has an additional "argument"—the amount to mutate the lvalue by—while the other has a "default" argument of +1. `++i`, on the other hand, is a surprising bit of syntax, the first time you see it. It takes (and mutates) an lvalue... on its right! None of the (few) other unary prefix operators in Algol-like languages take lvalues, so it's kind of a shock. (Also, for newcomers to programming, unary - [numeric negation] is commonly interpreted as part of the grammar of a number literal rather than being an expression; and unary ! is commonly seen as just "part of the language" (i.e. less like & and |; more like && and ||). Unary ~ is the only one I'd expect it would occur to any new programmer as even being a "unary prefix operator" rather than just "the way the language is." And it's a pure-functional transformation of its input.)
- jedimastert 8y ago> even though there are more objective reasons for the opposite, at least in C++ world. That's my question. What are those reason?
- paulddraper 8y agoI too prefer prefix operators due to simpler/more intuitive semantics. But postfix operators have simpler/more intuitive presentation. ++i and --i ressemble + +i and - -i Whereas i++ and i-- resemble abbreviations of i += 1 and i -= 1
- isaachier 8y agoGoogle style guide has me sort of leaning to always using ++i: https://google.github.io/styleguide/cppguide.html#Preincrement_and_Predecrement https://google.github.io/styleguide/cppguide.html#Preincreme...
- tokyodude 8y agoBecause you should write what you mean, not just assume the compiler will convert what you wrote into what you mean. i++ means var temp = i; i = i + 1; return temp; where as ++i means i = i + 1 return i; So if you actually mean to create a temp then use i++ , if you don't use ++i See example of actually implementing them https://stackoverflow.com/a/3846374 https://stackoverflow.com/a/3846374