4 ms·
To me, ++ is the one obvious way to increment. I use for loops constantly and think of incrementation as a different thing from addition.
by nix0n 11y ago
To me, ++ is the one obvious way to increment. I use for loops constantly and think of incrementation as a different thing from addition.
- koko775 11y agoTo be clear, do you mean ++i or i++?
- SilasX 11y agoWell yes, they are different from addition, but that still doesn't justify a special symbol for a special case of addition. When your "increment by 1" is really just in the service of iterating over a set, then the natural, obvious way should be to use a separate idiom that abstracts the concept of iteration over that set, rather than dive into the details of how you get access to the next member. Which is what Python does: "for x in ...". If the loop is not simply iterating over the set, but tweaking some value unpredictably and periodically checking, then there's nothing special about += 1 relative to += 2.
- msellout 11y agoWhy are you incrementing inside a Python for-loop? Are you familiar with ``enumerate``? In most situations, Python provides an iterator that tracks state for you and it's not necessary to increment state explicitly in the loop.
- nine_k 11y agoIn higher level languages, loops are not about incrementing.