-Are you referring to pre-TEST and post-TEST loops?
Pre-test loop is when you do some calculation or some function before checking to see if a condition is met. A post-test loop is when calculations are done after a condition is checked.
Pre-test loop (check condition before calculations are done)
while (x < 10) do
// some function
Post-test loop
do
// some function
until (x = 10)
Pre-test is useful if you want to check to determine if the calculations even need to be done BEFORE doing them. For example, if you write a program that checks the current date to find out if today is your birthday, you would check the date BEFORE throwing the party, so your code would look something like this:
if (today == (April 10)) then
ThrowBirthdayParty;
However, if you replace the above code with a post-test loop, you get this:
do
ThrowBirthdayParty
until (today <> (April 10));
What happens in the second example is that the birthday party is thrown BEFORE cheching to see if it needs to be thrown, which is what we don't want in this situation. However, if you do want to perform a function before any checking is done, this can be useful for situations like filling a bucket with grains of sand (when you know for a fact the bucket is empty to begin with).
do
TossInGrainOfSand
while (Bucket <> Empty);
Hope this helps!
没有评论:
发表评论