Programmer Guide > Statement Types > WHILE Statement
  

WHILE Statement
WHILE expression DO statement
WHILE statements are used to execute a statement repeatedly while a condition remains true. The WHILE statement is similar to the REPEAT statement except that the condition is checked prior to the execution of the statement.
When the WHILE statement is executed, the conditional expression is tested, and if it is true, the statement following the DO is executed. Control then returns to the beginning of the WHILE statement where the condition is again tested. This process is repeated until the condition is no longer true, at which point the control of the program continues at the next statement.
In the WHILE statement, the subject is never executed if the condition is initially false.
Examples of WHILE statements are:
WHILE NOT EOF(1) DO READF, 1, A, B, C
In this example, data are read until the end-of-file is encountered.
The next example demonstrates one way to find the first point of an array greater than or equal to a selected value assuming the array is sorted in ascending order (the array contains N elements):
; Determine number of elements in ARR.
N = N_ELEMENTS(ARR)
; Initializes index.
I = 0
; Increments I until a smaller point is found or the end of
; the array is reached.
WHILE (ARR(I) LT X) AND (I LT N)
DO I = I + 1
Another way to accomplish the same thing is with the statements:
; P is a vector of the array subscripts where ARR(I) GE X.
P = WHERE(ARR GE X)
; Saves first subscript.
I = P(0)
 

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.