Re: OT: Different types of counting in loops



Mario Testinori wrote:
This is not a fortran language question so let me apologize in advance
for that.
It interests me very much, and by lurking on this group for a while I
noticed it has a very no-nonsense approach, so I thought of posting it
here.

------
In fortran you have: for 10 i=1,5
which will do 5 iterations: 1,2,3,4,5

It is logical, it's probably the most "natural" way of thinking (you
usually count from one)

In C you have: for (i=1, i<5, i++)
although more common is: for (i=0, i<5, i++)
which will do 5 or 6 iterations.

In python you have:
for i in range(1,5):
print i
which will do 4 iterations (1,2,3,4)

If you want to get "1,2,3,4,5) you have to put range(1,6)
-------

Now, my two main questions are:
1. Why do all these languages do this in a different way? Was there
some practical reason for it, or purely a decision of the original
author of the language ?

2. Why have most of todays languages adopted counting/indexing from 0,
and not from 1 as fortran does? Mathematics learns that you always
start counting from 1 ... so surely there must be some reason?

As I said, this is not a fortran question. So I am offtopic.
But I was hoping if you could satisfy my curiosity, since when you
start mixing languages, this becomes a very problematic issue -
expecially if you have a habit of thinking one way, and the language
has a habit of acting some other way :-)

---
Mario
These aspects of Fortran were set up well before any other language had
demonstrated a practical alternative. The choices made 5 decades ago
worked well enough to remain in effect. For 3 decades, Fortran has
included a reasonable degree of support for the 0 based array alternative,
which has caught on mostly with C zealots.
C starts with primacy of pointers rather than arrays, with the equivalence
double *a;
a == &a[0];
a+1 == &a[1];
and spending years (at least until 1989) to surpass the degree of
portability which Fortran had 2 decades earlier.
C has always supported, without much publicity, the possibility
double *a;
double *b = a-1;
&b[1] == &a[0];
enabling traditional Fortran programmers to write C with identical int
ranges, at the potential cost of further confusion.

C defaults, of course, had more influence than Fortran on those which came
later. Going back a bit further, Algol had more influence on C and Pascal
than Fortran, and seemingly, the documentors felt a need to de-emphasize
the degree of influence which Fortran had, even though the first Pascal
was written in Fortran. Algol and its descendants, in turn, had some
influence on later Fortran standards, as well as many other languages.
.



Relevant Pages

  • Re: A question on Newtons Method
    ... > Jon Harrop wrote: ... >> archaic theoretical foundation, other languages aren't. ... >> forced to use old code, I see no reason to use Fortran. ... > repository there's every reason to join Fortran literacy club. ...
    (sci.math.num-analysis)
  • Re: Fortran now in the Great Language Shootout
    ... > representative of how Fortran compares to the other programming ... > languages than the current naive implementation in multiple languages. ...
    (comp.lang.fortran)
  • Re: Fortran bashing in the MATLAB newsgroup
    ... try to keep up with more modern languages. ... There is one sole reason for teaching Fortran ... I've got my own ideas about what modern Fortran is good for and why I ... As an example of item 1, if you're talking to C/C++ programmers, ...
    (comp.lang.fortran)
  • Re: thoughts on dynamic from a beginners perspective
    ... working engineering and heavy duty numerical codes from Fortran to ... writing such codes today. ... Interoperating between managed languages makes it trivial to ...
    (comp.programming)
  • Re: List of software programs written in fortran (for engineers and scientists)
    ... These sorts of programs were originally written in languages ... Fortran was THE implementation language ... to mention a few of the sites where DEUCE was installed. ... and DEUCE (re-engineered Pilot ACE) ...
    (comp.lang.fortran)