Yazoo ---> Online Help Docs ---> Yazoo scripting ---> Loops and if blocks

while and do until

A close cousin to the if block is the while loop. The difference is that the code between if and end if runs only once if the condition is true, whereas the code between a while and an end while runs repeatedly until the condition is false, at which point execution picks up after the end of the loop. Consider the following:


    counter := init_val
    while counter <= 5
       print(counter, " ")
       counter = that+1
    end while       | or endw
   
    print("-- done!\n")
   

If init_val is 1, then our program will output "1 2 3 4 5 -- done!". Suppose init_val is 6. Then our condition is never satisfied and we just get "-- done!".

A second, very similar loop, is the do-until loop. This executes the code between the do and the until as long as the condition is not satisfied. Transcribing our above example into do-loop form, we have


    counter = init_val
    do
       print(counter, " ")
       counter = that+1
    until counter > 5
   
    print("-- done!\n")
   

One difference between do and while is that to achieve the same effect we have to use the opposite condition in a do loop from that of a while loop. A more substantive difference is that, because the condition in a do loop is evaluated only at the end of the loop, we are guaranteed of getting at least one run before getting booted out. So if init_val is 6, we get the output "6 -- done!". Whether or not we always want this initial run usually determines the choice of while or do.

For convenience, the end-of-loop marker end while can be abbreviated endw. There is no shorthand for a do loop.


Prev: if   Next: for


Last update: July 28, 2013

Get Yazoo scripting language at SourceForge.net. Fast, secure and Free Open Source software downloads