3 ms·
I think also the construction of the for loop makes it more natural. Lua is "for i = i, n" vs the more complex and off-by-1 prone "for (i = 1; i < n; i++)" wher
by justincormack 14y ago
I think also the construction of the for loop makes it more natural. Lua is "for i = i, n" vs the more complex and off-by-1 prone "for (i = 1; i < n; i++)" where you need the less than vs an equality.
- unwind 14y agoIf your for loop is for a 0-based language, it's wrong because it starts with 1. If it's for a 1-based language, it's wrong because it compares with "<" and not "<=". So, I guess that proves your point that it's error-prone. :) In C, the idiomatic way is (as you surely know) "for(i = 0; i < n; ++i)".
- justincormack 14y agotypo, the second one was supposed to be 0 based... although that shows how off by 1 errors occur.
- qznc 14y agoThe unintuitive thing about this choice is that "for i = n,m" calls the loop body "m-n+1" times. The "for (i = n; i < m; i++)" does it "m-n" times. A more elaborate argument: http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831....