Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

integer do variable ending value ?

Status
Not open for further replies.

zmth

Technical User
Aug 5, 2015
57
PH
In an integer do loop such as

do i=lower,iupper
.... ; enddo

More specifically with an integer do loop variable default step 1 whose upper limit is atleast as great as the lowerwill it always be the case that after the loop normally exits, eg assuming nothing in the loop such that one may jump out of the loop etc., that the do loop variable will always be left 1 higher than the upper limit? So no matter how many statements intervene after the loop as long as the value of the do loop variable is not changed and assuming within the same program unit (within scope) is it guaranteed that this will always be the case ? For example in the above example i will be iupper+1 assuming iupper>=lower,
that i will equal iupper+1 ? Trials I have done always give this result.
 
For many implementations

do i = lower, upper, inc
...
enddo

i will be upper + inc but this is not guaranteed, especially if there is an exit do in the middle of the loop. You also need to check what happens when upper is the maximum integer. maximum integer + inc is undefined or may even wrap round, in which case you will end up with an endless loop.

Don't rely on this if you are using different compilers or working in multiple language because in some languages (like C++, java, C#, Algol68) the value is undefined outside the loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top