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

  for (i = 0; i < 10; i++) 
Why do so many language copy C's error prone while masquerading as a for loop? This construct is responsible for quite a bit of errors (off by 1).


What do you consider a for loop to be? The loop constructs called "for" that existed prior to C all just iterated through a numeric sequence with an optional step parameter. This was present in BASIC, ALGOL68 and Pascal.

The C for loop was designed by looking at an ALGOL-style for loop:

    FOR i FROM 1 BY 2 TO 3 WHILE i≠4 DO ~ OD
and trying to make the construct not tied to integer sequences (because such a specific construct would be out of place in C). The typical for loop, like the one in your comment, has a begin, end and increment parameter, just like the ALGOL example.

I assume you mean that you'd prefer some kind of for-each kind of loop, but in order to support that, a language needs a formalized concept of iterators, which may be out of scope for the project.


The for in my example (which is directly from the linked page) is a while loop written in a compact form. My complaint is that a for loop should name a definite end number without a condition. I think the WHILE portion of the ALGOL-style for loop you give is problematic (and given the example unused). Having a condition as an end condition is error prone.




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

Search: