next up previous contents
Next: The WHILE loop Up: Flow Control Previous: The IF statement   Contents

The FOR loop

If you have a statement or statements that you want to repeat a number of times, you can use the FOR statement to do so. It looks like this:

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

( I use [ ] to indicate that increment is optional.) The variable will start at start_value and statement will be executed over and over again, with increment being added to variable each time, until variable reaches stop_value. If you miss out increment it is assumed to be 1.

Try these examples at the IDL prompt.

IDL> for j=0,5 do print,j
IDL> for j=0,6,2 do print,j

Just as a warning, 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.



John Marsham 2005-04-22