Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


4-5 The WHILE Loop

The WHILE loop construct contains the necessary condition to terminate the loop, but unlike the FOR loop, no counter is included. It takes the general form:

    WHILE condition DO
        statement;

This form simply says: “Execute the following statement as long as the condition is TRUE.”

When the loop is entered, the condition (a boolean expression) is evaluated. If it is TRUE, the statement that follows the keyword DO is executed. The loop will be repeated, and the statement will be re-executed until the condition becomes FALSE. In your program, you must include the necessary logic to make the condition FALSE at the right time. You may use a counter with this loop, but you need to increment or decrement the counter yourself.

The following program demonstrates the same algorithm of calculating the average of a set of numbers entered from the keyboard but uses the WHILE loop. The condition is here used to test the value of a counter “Kounter” against the maximum number of elements “N.” When this maximum is reached the loop exits.

{ ------------------------------ figure 4-7 ------------------------------ }
PROGRAM AverageProg2(INPUT,OUTPUT);
VAR
 Average, Sum, Number:REAL;
 Kounter, N     :INTEGER;
BEGIN
 Sum := 0;
 Kounter := 1;
 WRITE('Enter Number of Elements:');
 READLN(N);
 WHILE Kounter <= N DO
  BEGIN
   WRITE('Enter Element #',kounter,': ');
   READLN(Number);
   Sum := Sum + Number;
   Kounter := Kounter + 1
  END;
 Average:= Sum / N;
 WRITELN;
 WRITELN('Sum of Numbers = ', Sum:0:2);
 WRITELN('Average of Numbers = ', Average:0:2);
 WRITELN;
 WRITELN('Press ENTER to continue..');
 READLN
END.

Notice that the counter is initialized at the beginning of the program and incremented inside the loop. The initial value is used for the first round in the loop (Kounter := 1), because the incrementing takes place after the process. This is one way to do it, but other arrangements are used in the next few programs. Notice also that when you want to include more than one statement in the WHILE loop, you must use the BEGIN-END blocks.

The following is a sample run of the program:

Enter Number of Elements: 3
Enter Element #1: 1
Enter Element #2: 2
Enter Element #3: 3

Sum of Numbers = 6.00
Average of Numbers = 2.00

Press ENTER to continue..

If you do not want to enter the number of elements beforehand, you can count them inside the loop. In this case you need a clue to end the loop, like entering a negative number. Look at this modified version of the program, where the input number is tested with every round to see if it is “-1.”

{ ------------------------------ figure 4-8 ------------------------------ }
PROGRAM AverageProg3(INPUT,OUTPUT);
VAR
 Average, Sum, Number:REAL;
 Kounter        :INTEGER;
BEGIN
 Sum := 0;
 Average := 0;
 Number := 0;
 Kounter := 0;
 WHILE Number <> -1 DO
  BEGIN
   Kounter := Kounter + 1;
   Sum := Sum + Number;
   WRITE('Enter element #',kounter,' (or -1 to end): ');
   READLN(Number)
  END;
 IF Kounter > 1 THEN
  Average := Sum / (Kounter - 1);
 WRITELN;
 WRITELN('Sum of Numbers = ', Sum:0:2);
 WRITELN('Average of Numbers = ', Average:0:2);
 WRITELN;
 WRITELN('Press ENTER to continue..');
 READLN
END.

Drill 4-4

Use the WHILE loop construct to write a program to display a multiplication table as in the following example:

    1 * X = Y
    2 * X = Y
    3 * X = Y
    4 * X = Y
    5 * X = Y
    6 * X = Y
    7 * X = Y
    8 * X = Y
    9 * X = Y
    ...

The value of “X” is received from the keyboard and the value “Y” is the multiplication result.

The following is a sample run:

Enter element #1 (or -1 to end): 1
Enter element #2 (or -1 to end): 2
Enter element #3 (or -1 to end): 3
Enter element #4 (or -1 to end): -1

Sum of Numbers = 6.00
Average of Numbers = 2.00

Press ENTER to continue..

Notice the following points in this program:

  The input statement comes at the end of the loop block so that the input value can be tested before any processing.
  The “Average” is calculated by dividing the sum by the value of the counter decremented by one. This is to counteract the extra round which took place when the “Number” was “-1.”
  The “Average” is calculated only if the variable “Kounter” is not equal to “1.” This is to avoid the “divide by zero” error, in case you want to exit the program without entering any data. In such a case you would get the following response:
Enter element #1 (or -1 to end): -1
Sum of Numbers = 0.00
Average of Numbers = 0.00

Press ENTER to continue..

4-6 The REPEAT Loop

This loop is used to execute a group of statements until a specified condition is met. It takes the form:

    REPEAT
        statement-1;
        statement-2;
        ...
        statement-n;
    UNTIL condition;


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.