- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is a question I've wondered for a long time:
Can the beginning index for a Do-loop ever be <1 (e.g., 0)?
In recent practice, I discovered that using 0 as a starting value causes a crash, e.g., :
! Initialize several variables for later use: do j = 0,N TwLev(j) = Tw_out + j*DeltaTwLev WaLev(j) = Wa_in HaLev(j) = Ha_in enddo
forrt1: severe (408) : fort: (3): Subscript #1 of the array TwLev has value 0 which is less than the lower bound of 1
The error message seems to indicate that 1 is the lowest acceptable integer. Any thoughts on this?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The beginning (and/or end) of a do loop can be less that zero. Array indices can also be less than zero, if the bounds for the array are declared/defined appropriately.
Your issue is highly likely to be that the lower bound of your array is the default value of 1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hmmmm ... Not sure what you mean by "the lower bound of yo array is the default value of 1." That 1-dimensional array is defined at size 10. I wonder what I could do to use a lower index of 0. Mind you, 99.99% of the time, I'd use a value of 1but in this case (for reasons I won't waste time getting into), I may have to use 0. I appreciate your thoughts, Ian.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There is nothing mysterious about such index bounds:
do i = -100,100
write(*,*) i
enddo
for instance would print the numbers -100, -99, ... 99 and 100 in that order.
If you want to access an array element with index 0, which you do in the original loop, then the array should be declared as having such an index. For instance:
real, dimension(-100:100) :: a
Then a(0) is within the bounds of the array. You can even use element -100.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
TommyCee wrote:
Hmmmm ... Not sure what you mean by "the lower bound of your array is the default value of 1." That 1-dimensional array is defined at size 10. I wonder what I could do to use a lower index of 0.
Read the appropriate section of the Fortran Standard (5.3.8.2 of Fortran 2008, "Explicit-shape array") or a Fortran guide. Whether the array element appears in a DO loop or not is irrelevant to the declaration of the array. Likewise, the use of a DO index variable as a subscript expression or in a subscript expression is allowed but is not required. DO index variables and subscripts of variables used in the loop may have any permitted integer values, with no relation being implied between the DO variable and the subscripts, unless you impose such a relation.
When you declare an array, for each extent of the array you can declare the lower and upper bounds. If you do not declare the lower bound, it is assumed to be 1. Thus A(10), A(1:10), A(0:9) and A(-5:4) are all rank-1 arrays of length 10.
You have to be careful if you declare arrays with lower bound(s) different from 1 and pass them as actual arguments to subroutines, because the lower bound (unless it is the default value of 1) may not be available in the subroutine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks ArjenMarkus & mecej4. In your responses, you pointed out a subtle construct that I once may have known but had long forgotten. As I said at first, in 99.9% of my applications, I expect the bottom index to be 1.
The short answer to my question for lay people is:
For the declaration Array(y), the intrinsic assumption (default) is that the bottom index is 1. If a bottom index <1 is intended, the array must be declared as: Array(x:y), where x <1
A(10), A(1:10), A(0:9) and A(-5:4) are all rank-1 arrays of length 10
also serves as a good example.
As always, I appreciate the responses, my Fortran friends.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
TommyCee wrote:
The short answer to my question for lay people is:
For the declaration Array(y), the intrinsic assumption (default) is that the bottom index is 1. If a bottom index <1 is intended, the array must be declared as: Array(x:y), where x <1
And similarly, If a bottom index > 1 is intended, the array must be declared as: Array(x:y), where x > 1.
What is at issue is not the sign of x, but x being different from 1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Well said, mecej4. Thanks again!

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page