3 ms·
Why 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 cons
by eMSF 8y ago
Why 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