Explain the difference between a pretest loop and a posttest

Explain the difference between a pre-test loop and a post-test loop and give one example of each. Indicate a situation where it might be more advantageous to use a post-test loop over a pre-test loop.

Solution

The difference between pre-test loop and a post-test loop are let us consider pre test Do loop and post test Do loop.

In the pre-test Do-while tests the loop condition each time through the loop & it keeps executing while the test expression is a true value.

In the post-test Do-while Loop while we check the condition at the bottom,

In the pre-test Do-while loop When the conditional expression is false then the statements in Do/Loop are skipped.

In the post-test Do-while loop When the loop while is used ,the body of loop is executed at least once, since the condition is at the bottom of the loop.

Example of Pre-Test loop:

Do While intNumber < 100

lblNumber.Caption=intNumber

intNumber = intNumber + 1

Loop

Example of post-test loop:

Dim intNum as integer

Do

Form1.print intNum

intNum = intNum +1

Loop while(intnum <= 10)

In a pre-test loop the control variable is tested prior to the start of each iteration in post-test loops the control variable is tested at the end of each iteration. From an efficiency point of view the often stated advantage is that we can reduce the number of tests of the loop control variable/parameter provided that we know that the loop must be exercised at least once. If there is a possibility that the loop should not be exercised at all then a post-test loop will not be appropriate (a pre-test loop will be required instead).

Explain the difference between a pre-test loop and a post-test loop and give one example of each. Indicate a situation where it might be more advantageous to us

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site