WHILE...DO loops are used to execute a statement repeatedly while a condition remains true. The WHILE...DO statement is similar to the REPEAT...UNTIL statement except that the condition is checked prior to the initial execution of the statement. See Definition of True and False for details on how the “truth” of an expression is determined.
Syntax
WHILE expression DO statement
or
WHILE expression DO BEGIN
statements
ENDWHILE
Examples
i = 10
WHILE (i GT 0) DO PRINT, i--
IDL prints out the numbers 10, 9, ...1.
The following example reads data until the end-of-file is encountered:
WHILE ~EOF(1) DO READF, 1, A, B, C
The subject statement can also be in the form of a block:
WHILE ~EOF(1) DO BEGIN
READF, 1, A, B, C
ENDWHILE
The next example demonstrates one way to find the first element of an array greater than or equal to a specified value assuming the array is sorted into ascending order:
array = [2, 3, 5, 6, 10]
i = 0
n = N_ELEMENTS(array)
WHILE (array[i] LT 5) && (i LT n) DO i++
PRINT, 'The first element >= 5 is element ', i
IDL Prints:
The first element >= 5 is element 2
Tip: Another way to accomplish the same thing is with the WHERE function, which is used to find the subscripts where the array is greater than or equal to X.
P = WHERE(array GE X)
Version History
See Also
BEGIN...END,
BREAK, CASE,
CONTINUE, FOR,
FOREACH, GOTO,
IF...THEN...ELSE,
REPEAT...UNTIL,
SWITCH
IDL Programming