|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
4-5 The WHILE LoopThe 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. 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:
4-6 The REPEAT LoopThis 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;
|
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. |