Repetition Form: The WHILE statement - Pascal programming

The syntax of the Pascal WHILE loop is shown in the syntax diagram at the lef of Figure. At the right of the same figure is a template that shows how the statement should be written in a Pascal program, when the body of the loop includes more than one statement.

Syntax diagram and template for Pascal WHILE Statement

Syntax diagram and template for Pascal WHILE Statement

The Pascal WHILE statement consists of a single sequence: the word WHILE, followed by a logical expression, then the word DO, and finally a simple statement that serves as the body of the loop. Usually, the body involves more than one statement, and in that case the single statement is replaced by a compound statement. Remember that a compound statement is a group of statements enclosed within a BEGIN -END pair. The template shown at the right of Figure covers this specific case. The semantics for the While statement match those of the Repetition form and are defined by the flowchart in Figure

Flowchart showing semantics of While statement

Flowchart showing semantics of While statement

Figure shows a typical example of a Pascal WHILE loop that computes the factorial of N.

Loop to compute the factorial

Notice how we use the indentation to show the extent of the body of the loop. With this format, the WHILE is aligned with the corresponding END and the body of the loop is clearly visible.

Loop to compute the square

The segment of Pascal program shown in Figure shows how the square of variable N can be computed by summing the first N odd integers.

Loop to enter valid data

Figure illustrates a very useful method for entering valid data. First, the user is prompted to enter the Age. Then, while the Age is negative (not a reasonable value for an age), the user is asked to re-enter the value. When a proper value has been entered, it is echoed to confirm that the intended value was actually received.

Loop to force user to order data

The segment of Pascal program shown in Figure 5.10 is a similar example: the user is prompted to enter three values that are in increasing order. The program does not continue further unless the values are in order. This example illustrates a program that is not very user-friendly. The user should not have to enter data in order, for that could be done easily by the computer (remember we have done it in the Triangle example of Chapter 4)!

Tracing Loops

The tracing of the execution of loops by hand was considered in the Principles. Tracing loops in this way often provides insight, suggests modifications and is a very effective way to detect errors. It is also a useful way to show relations between variables, and to find loop invariants. However, such tracing by hand is frequently tedious and error-prone. Because it is so labor intensive, it is usually limited to only a few loops. Using the computer to help in the tracing can be equally useful and is much more convenient. We’ll do it on an example algorithm seen in Chapter 5 of the Principles book. We reproduce here the algorithm pseudocode taken from Figure.

Input Amount, Duration, Payment, RateSet Balance to AmountSet Time to 0While Time < DurationSet Interest to Rate Balance choppedSet Balance to Balance + Interest – PaymentSet Time to Time + 1Output Balance

This algorithm computes the Balloon Payment of a loan, and Figure shows a Pascal version for it. However, an error was introduced during the translation into Pascal, and we have left it here!.

Pascal program for Balloon Payment of a loan

Notice that, in the Pascal version of Figure, the input of the data and the output of the results is now a much larger proportion of the whole algorithm. This is typical of programs that have interaction with the user. Notice also that some statements to output tracing information that appear in the Pascal program were not part of the original algorithm. These extra statements have been sandwiched between comments with many asterisks. If we run that program with the data we used in the trace of the original algorithm, we obtain the following:

Enter amount of loan: 600 Enter payment amount: 100 Enter the duration in months: 6 Enter annual interest rate as a decimal percent: 12 2 Balance = 506 3 Balance = 411 4 Balance = 315 5 Balance = 218 6 Balance = 120 Balloon Balance is: 120 Total interest is : 20

This output contains information produced by the trace statements that hav been added to the program. Notice that this trace “grows” downward, as opposed to the hand -trace, shown in Chapter of the Principles book, which grew towards the right. The final resulting Balloon payment seems reasonable but there are problems! The trace information shows us that the program executes the loop 5 times only, rather than the required 6 times; furthermore, the count values in the trace start at 2, whereas we expected them to start at 1. The trace output has allowed us to detected an error, an “off-by-one” error, which is very common. A remedy is to initialize the Time to zero before entering the loop —as was done in the pseudocode! After this correction, the program produces:

Enter amount of loan: 600 Enter payment amount: 100 Enter the duration in months: 6 Enter annual interest rate as a decimal percent: 12 1 Balance = 506 2 Balance = 411 3 Balance = 315 4 Balance = 218 5 Balance = 120 6 Balance = 21 Balloon Balance is: 21 Total interest is : 21

This time, the loop is executed the correct number of times and the values in the Time field start at the more reasonable value of 1. Notice that the final Balance equals the total interest, which is a nice check. This program, with its trace, can be run for other values of input like the following which checks the interest -only loan mentioned.

Enter amount of loan: 600 Enter payment amount: 6 Enter the duration in months: 6 Enter annual interest rate as a decimal percent: 12 1 Balance = 600 2 Balance = 600 3 Balance = 600 4 Balance = 600 5 Balance = 600 6 Balance = 600 Balloon Balance is: 600 Total interest is : 36

A more realistic set of values would involve larger amounts, monthly payments over 5 years, and the resulting trace would have 512 or 60 lines. Such large amounts of output in a trace are likely to be overwhelming, so the trace could be modified to output only every fifth value. We could also output some additional information, the values of Interest and IntSum, as a table by inserting the following Selection form:

New set of trace statements

After replacing the previous trace statements of the Loan program by the more detailed trace statements of Figure, the following output was obtained from a run:

Enter amount of loan: 12000 Enter payment amount: 200 Enter the duration in months: 60 Enter annual interest rate as a decimal percent: 12 Num Bal Int Sum 5 11590 116 590 10 11159 112 1159 15 10704 107 1704 20 10229 103 2229 25 9729 98 2729 30 9204 93 3204 35 8650 87 3650 40 8069 81 4069 45 7458 75 4458 50 6816 69 4816 55 6141 62 5141 60 5432 55 5432 Balloon Balance is: 5432 Total interest is : 5432

The debugging of programs is often done by judiciously placing such Write statements within programs. The statements need not just write values, but could also write messages as in:

Other outputs could also involve the loop invariants or the use of a tool called a debugger, which will be introduced later.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Pascal programming Topics