Re: OT: Different types of counting in loops
- From: Tim Prince <tprince@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 05 Oct 2008 18:23:21 -0700
Mario Testinori wrote:
This is not a fortran language question so let me apologize in advanceThese aspects of Fortran were set up well before any other language had
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
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.
.
- Follow-Ups:
- Re: OT: Different types of counting in loops
- From: David Thompson
- Re: OT: Different types of counting in loops
- From: Gary Scott
- Re: OT: Different types of counting in loops
- References:
- OT: Different types of counting in loops
- From: Mario Testinori
- OT: Different types of counting in loops
- Prev by Date: Re: OT: Different types of counting in loops
- Next by Date: Re: OT: Different types of counting in loops
- Previous by thread: Re: OT: Different types of counting in loops
- Next by thread: Re: OT: Different types of counting in loops
- Index(es):
Relevant Pages
|