Jump statements can be used to modify the behavior of conditional and iterative statements. Jump statements allow you to exit a loop, start the next iteration of a loop, or explicitly transfer program control to a specified location in your program.

Statement Labels

Labels are the destinations of GOTO statements as well as the ON_ERROR and ON_IOERROR procedures. The label field is an identifier followed by a colon. Label identifiers, as with variable names, consist of 1 to 15 alphanumeric characters, and are case insensitive. The dollar sign ($) and underscore (_) characters can appear after the first character. Some examples of labels are as follows:

I$QUIT: RETURN   ;Comments are allowed.


The BREAK statement provides a convenient way to immediately exit from a loop (FOR, FOREACH, WHILE, REPEAT), CASE, or SWITCH statement without resorting to the GOTO statement.


This example illustrates a situation in which using the BREAK statement makes a loop more efficient. In this example, we create a 10,000-element array of integers from 0 to 9999, ordered randomly. Then we use a loop to find where in the array the value 5 is located. If the value is found, we BREAK out of the loop because there is no need to check the rest of the array:

Note: This example could be written more efficiently using the WHERE function. This example is intended only to illustrate how BREAK might be used.

; Create a randomly-ordered array of integers
; from 0 to 9999:
array = SORT(RANDOMU(seed,10000))
n = N_ELEMENTS(array)
; Find where in array the value 5 in located:
FOR i = 0,n-1 DO BEGIN
   IF (array[i] EQ 5) THEN BREAK

We could write this loop without using the BREAK statement, but this would require us to continue the loop even after we find the value we’re looking for (or resort to using a GOTO statement):

FOR i = 0, n-1 DO BEGIN
   IF (array[i] EQ 5) THEN found=i
PRINT, found


The CONTINUE statement provides a convenient way to immediately start the next iteration of the enclosing FOR, FOREACH, WHILE, or REPEAT loop. Whereas the BREAK statement exits from a loop, the CONTINUE statement exits only from the current loop iteration, proceeding immediately to the next iteration.

Note: Do not confuse the CONTINUE statement described here with the .CONTINUE executive command The two constructs are not related, and serve completely different purposes.

Note: CONTINUE is not allowed within CASE or SWITCH statements. This is in contrast with the C language, which does allow this.


This example presents one way (not necessarily the best) to print the even numbers between 1 and 10:

   IF (I AND 1) THEN CONTINUE  ; If odd, start next iteration
      PRINT, I


The GOTO statement is used to transfer program control to a point in the program specified by the label. The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided. However, for cases where using GOTO is appropriate, IDL does provide the GOTO statement.

Note that using a GOTO to jump into the middle of a loop results in an error.

The syntax of the GOTO statement is as follows:

GOTO, Label

Note: You must be careful in programming with GOTO statements. It is not difficult to get into a loop that will never terminate, especially if there is not an escape (or test) within the statements spanned by the GOTO.


In the following example, the statement at label JUMP1 is executed after the GOTO statement, skipping any intermediate statements:

PRINT, 'Skip this' ; This statement is skipped
PRINT, 'Skip this' ; This statement is also skipped
JUMP1: PRINT, 'Do this'

The label can also occur before the GOTO statement that refers to the label, but you must be careful to avoid an endless loop. GOTO statements are frequently the subjects of IF statements, as in the following statement: