20. Writing 4GL Statements : Flow-Control Statements : Using the While Statement
 
Share this page                  
Using the While Statement
The while statement specifies a list of 4GL statements to be repeated while a given condition is true. The condition is tested at the start of each pass through the loop; if the condition is true, then the statements in the loop are executed. The endloop statement can be used to break out of the loop at any time, as shown in the next section.
The example below repeats a prompt five times or until a valid y (yes) or n (no) response is given:
answer := ' '; 
requests := 0; 
while (lowercase(left(answer,1)) != 'y'
  and lowercase(left(answer,1)) != 'n'
  and requests < 5) do 
  answer := prompt 'Please answer Y or N: '; 
  requests := requests + 1; 
endwhile;
In this example, the prompt command places the user's response into the answer field.
The while statement can also set up a loop which terminates with an endloop or resume statement.