Hi:
If you run both programs, as suggested, you'll see that the results of both programs are the same whether you use a WHILE or an UNTIL:
However, look at the condition that was needed to make each DO loop work to produce the same results. The UNTIL code uses (Capital GT 500000) The UNTIL will ALWAYS have the loop executed at least once because the condition is checked at the bottom of the DO loop, after the statements have executed at least one time.
The condition for the WHILE was (Capital LE 500000) and the condition for a WHILE is checked at the top of the loop. So in this example, both loops would run the same number of times to produce the same results.
If you have data and some logic that needs to be executed at least one time if the initial condition is met, then use a DO UNTIL. If you have some logic that you do NOT want to execute if the initial condition is met, then use a DO WHILE. This first example shows that both forms of the DO loop are capable of producing the same results, given that you have coded the condition correctly.
However, to prove that it is possible for the 2 loops to produce different results, what if in the example program, we started the value for Capital at exactly 500000 and what if we changed the logical operator in the condition for the WHILE -- so that instead of testing for LE, we just tested for LT -- then the results WOULD be different:
Now you see that the program with DO UNTIL produced different results than the program with DO WHILE. This is not always the case, as the first program proves. You are in control of the operators used for the DO loop conditions.
The important things to learn about DO UNTIL/DO WHILE are: where does the condition for the loop get tested? Then, for the data and problem you are working with, do you need for the statements to execute at least one time or not? Finally, what logical operator do you need to use to make your loop work with your data? In the first program, we wanted to illustrate how it was possible for both forms of the DO loop to produce the same results. The key to making that happen was the logical operator we used in the code for the question.
Hope this helps explain things a bit more.
Cynthia
... View more