Why when making a loop do you use
Instead ofCode:for (int i = 0; i< 9; i++) { blahblahblah }
ThanksCode:for (int i = 0; i < 9; i + 1) {blahblahblahblah }
AL
Why when making a loop do you use
Instead ofCode:for (int i = 0; i< 9; i++) { blahblahblah }
ThanksCode:for (int i = 0; i < 9; i + 1) {blahblahblahblah }
AL
In your example, one will work and the other will produce an infinite loop (you need i+= 1 instead of i+1, since the latter doesn't update i). But after fixing that, there's no real reason to prefer one over the other.
In some contexts, it can save some typing :
orCode:while (i < len) array[i++] = 10;
compared toCode:while (++i < len) array[i] = i;
In more complex objects, pre-incrementing/decrementing (--a, ++a) can be slight more efficient than adding one since the compiler might be able to avoid allocating temporaries to do the math. But this shouldn't be a major concern - a decent compiler can eliminate most of the overhead in many cases.Code:while (i < len) { array[i] = 10; i += 1; }
Loops which advance an index on each iteration should usually be written as for-loops, not while-loops. Loops where the index is incremented in some bizarre spot in the middle of the loop, hidden inside the argument list to a function call, etc can be infuriating to debug and difficult to maintain.
It may not seem so bad with the above example, but imagine a 30 line loop with complicated computations and if-statements, maybe some nested loops, and somewhere in there is buried an "i++".
If i is incremented regularly, every time through the loop, then say so by writing a for-loop, assuaging my doubts and fears that something bizarre is going on that I don't understand.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
In terms of i++ and i+=1, they are both pretty readable, and will generally be compiled into the same thing.
It's really your call, but because the increment operators are part of the language, I think it's more clear to use them.
Not precisely true. Some machines support instructions for incrementing and decrementing, that are distinct from addition and subtraction.
While, in principle, compilers can (and often do) reduce incrementing and adding of one to the same set of operations, they are not guaranteed to do so.
Generally, when looping over arrays, I prefer to use ++x over x += 1. My main reason is that, generally, I prefer to avoid "magic values" in code, even in simple cases like this (the expression x += 1 results in a magic value of 1 visible in code, ++x does not, even if the two expressions achieve the same thing).
Another important aspect of ++,-- is that the l-value is only evaluated ONCE.
So if you have a complicated expression, the result is simpler code.Code:#include <stdio.h> int here ( ) { printf("here called\n"); return 0; } int there ( ) { printf("there called\n"); return 0; } int main ( ) { int arr[10] = { 0 }; arr[here()]++; arr[there()] = arr[there()] + 1; return 0; }
Also, you're not likely to end up with
arr[complex_edited_expression] = arr[complex_original_expression] + 1;
bugs.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.