3 ms·
Reminds me of the 'goes to' operator: for (int i = 42; i --> 0;) { foo(); }
by zokier 15y ago
Reminds me of the 'goes to' operator:
for (int i = 42; i --> 0;) { foo(); }
- eridius 15y agoThat's really cute, but it's also buggy. The loop will start at i == 41.
- jacquesm 15y agoYou are right about it starting on 41, but chances are that that is its intended behavior, with a bit of context that is more clear: #define ARRAYSIZE 42 int array[ARRAYSIZE]; void f() { for (i=ARRAYSIZE; i --> 0;) { printf("%d\n", array[i]); } } If it were to start at 42 you'd be referencing the array outside of its bounds.
- TazeTSchnitzel 15y agoIn C you're used to less-than-one loops though. for(i = 0; i < 5; i++) terminates on 4. And it's what you want if you're going through an array.
- delinka 15y agoThe observant (yeah, newbies aren't so much, typically) will note that it really shouldn't be called "goes to" because it doesn't work the other way: for(int i=0; i --> 42;) { bar(); }