If you need a loop for which you don't know in advance how many iterations there will be, you can use the `while' statement.
while condition do statement
Again, if you are using this in a program you can replace statement with a block of statements - the block must end with an endwhile . You can construct condition in the same way as for an if statement.
Here is an example (modtran_while.pro), but this time we don't know the number of lines in advance.
;Set up array with far too many rows to contain the data
modtrandata=fltarr(14,5000)
openr,1,'~lecjm/public_html/Teaching/IDL_course/Data/pldat.dat'
inputline = fltarr(14) ; set up a variable to hold one row of the file
linecount=0 ; variable to count lines
;;;; start while loop to read in the data
while not eof(1) do begin ; eof becomes true when we reach the End Of
; the File.
readf,1,inputline
modtrandata(*,linecount)=inputline
linecount=linecount+1
endwhile
modtrandata=modtrandata(*,0:linecount-1) ; truncate array to actual length
; of file.
close,1
OR (modtran_while2.pro)
pro modtran_while2,modtrandata
openr,1,'~lecjm/public_html/Teaching/IDL_course/Data/pldat.dat'
inputline = fltarr(14) ; set up a variable to hold one row of the file
linecount=0 ; variable to count lines
;;;; start while loop to read in the data
while not eof(1) do begin ; eof becomes true when we reach the End Of
; the File.
readf,1,inputline
if linecount eq 0 then modtrandata=inputline else modtrandata=[[modtrandata],[inputline]]
linecount=linecount+1
endwhile
modtrandata=modtrandata(*,0:linecount-1) ; truncate array to actual length
; of file.
close,1