1

I am trying to do a multiline for loop in IDL but its not working, here is my first program:

for n=0,5 do begin
  print, n

which gives this output:

6

and this is my second program:

for n=0,5 do begin
  print, n
endfor

which gives the following error:

endfor
 ^
% Syntax error.

I would like the program to gives this output:

0
1
2
3
4
5

Thanks

mgalloy
  • 2,356
  • 1
  • 12
  • 10
Mathew
  • 1,116
  • 5
  • 27
  • 59

3 Answers3

1

The proper way to write a multi-line FOR loop is indeed:

for n=0,5 do begin
  print, n
endfor

But IDL doesn’t accept multi-line statements with BEGIN/END blocks in all contexts. For instance, not directly at the command-line. If you want to do that at the command-line, you would need to create a small main-level program by doing:

IDL> .run
for n=0,5 do begin
  print, n
endfor
end

You could also use a FOR loop like that in a procedure or function.

mgalloy
  • 2,356
  • 1
  • 12
  • 10
1

as @SofCh above suggests, i just use the continuation characters to do this

for n=0,5 do begin $ 
  print, n & $ 
endfor
eeny
  • 111
  • 1
  • 3
0

If you're writing it in the command line you may need to add a '&' or '&$' at the end of the 'DO BEGIN' line. This indicates that the loop continues on the next line.

But also its usually easier to just condense it to one line, e.g.

    for n = 0,5 do print, n 
SofCh
  • 19
  • 2