      subroutine donest(ib,ie,is,n,i)
      dimension ib(n),ie(n),is(n),i(n)
* Determine if the loops are doable and the exact right bounds
      do 2 j=1,n
        if (is(j).eq.0) return
        k = (ie(j)-ib(j))/is(j)
        if ( ib(j).le.ie(j) .and. is(j).gt.0 ) then
          ie(j)=ib(j)+ k*is(j)
        elseif   ( ib(j).ge.ie(j) .and. is(j).lt.0 ) then
          ie(j)=ib(j)+ k*is(j)
        else
          return
        endif
 2    continue
* Set initial values
      do 3 j = 1,n
        i(j) = ib(j)
 3    continue
* Print all values now (running the innermost loop
 4    continue
      do 1 in = ib(n),ie(n),is(n)
         i(n) = in
         write(*,*)(i(j),j=1,n)
 1    continue
*
      do 10 j = n-1,1,-1
        if ( i(j).ne.ie(j) ) then
          i(j) = i(j)+is(j)
          go to 4
        else
          i(j) = ib(j)
        endif
 10   continue
      end
*
      program test
      parameter (m=4)
      dimension ib(m),ie(m),is(m),i(m)
      data ib/1,1,1,1/
      data ie/3,4,-3,2/         
      data is/1,2,-2,1/
      write(*,*) 'Enter a number >=1,and <=',m
      read(*,*)n
      if (n.gt.m) stop
      if (n.lt.1) stop
      call  donest(ib,ie,is,n,i)
      end
