next up previous contents
Next: The WHILE loop Up: Flow Control Previous: Conditional blocks   Contents

The FOR loop

for variable = start_value , stop_value [ , increment ] do statement

( I use [ ] to indicate that increment is optional.) e.g.

IDL> for i = 0,10 do print,i
       0
       1
       2
       3 etc
IDL> for j=0,6,2 do print,j
       0
       2
       4
       6
; OR 
for i = 0,10 do begin
  print,i
  print,i+2
endfor
( I use [ ] to indicate that increment is optional.)

Warnings:

  1. The loop variable takes its type from the start value. This is a (short) integer in these examples. This means that for j=0,40000 do .... will NOT do what you expect, but for j=long(0),40000 do .... will be OK.
  2. Loops are slow, but are still sometimes the best way to do things (and not actually the root of all evil)



John Marsham 2009-12-07