Welcome to the Harris Geospatial product documentation center. Here you will find reference guides, help documents, and product libraries.


Harris Geospatial / Docs Center / Using IDL / Jump Statements

IDL

Jump Statements

Jump Statements

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 simply 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:

LABEL1:
LOOP_BACK: A = 12
I$QUIT: RETURN   ;Comments are allowed.

BREAK


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.

Example

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
ENDFOR
 
PRINT, i

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
ENDFOR
 
PRINT, found

CONTINUE


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.

Example

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

FOR I=1,10 DO BEGIN
   IF (I AND 1) THEN CONTINUE  ; If odd, start next iteration
      PRINT, I
ENDFOR

GOTO


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 those cases in which the use of a 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.

Example

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

GOTO, JUMP1
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:

IF A NE G THEN GOTO, MISTAKE



© 2017 Exelis Visual Information Solutions, Inc. |  Legal
My Account    |    Buy    |    Contact Us