Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



The 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....


If 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)".


typo, the second one was supposed to be 0 based... although that shows how off by 1 errors occur.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: