|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Example: Powers of TwoThe number 2 and its powers are very important numbers in the computer field. Some of the numbers, such as 1024 Bytes (equivalent to 1 KB) and 65,536 Bytes (64 KB) are commonly used. In the following program a FOR loop is used to display the powers of two, using the same logic which was used to calculate the power in program 2-2. The program output gives the power and the number 2 raised to this power. The initial and final values of the counter are supplied by the user during the execution. Thus, you can determine the range of numbers you would like to examine. { ------------------------------ figure 4-3 ------------------------------ } PROGRAM ForLoop(INPUT, OUTPUT); VAR Base, Power, Start, Final:INTEGER; BEGIN Base := 2; WRITE('Enter starting exponent:'); READLN(Start); WRITE('Enter ending exponent:'); READLN(Final); WRITELN; WRITELN('Number Power of two'); FOR Power:= Start TO Final DO BEGIN WRITE(Power:3); WRITELN(EXP(LN(Base)*Power):20:0) END; WRITELN; WRITELN('Press ENTER to continue..'); READLN END. The following is a sample run using exponent values from 1 to 20:
Example: The AverageThe following program demonstrates data entry using a loop. It receives from the keyboard a series of numbers and calculates the sum and the average of the numbers. At the beginning of the program you are asked to enter the number of the elements N, which is used as the final value of the counter. Inside the loop the sum is accumulated in the variable Sum using the statement: Sum := Sum + Number; When the loop exits, the average is calculated from the sum and the number of elements, using the statement: Average := Sum / N; Here is the program. { ------------------------------ figure 4-4 ------------------------------ } PROGRAM AverageProg1(INPUT,OUTPUT); VAR Average, Sum, Number:REAL; N, Kounter :INTEGER; BEGIN Sum := 0; WRITE('Enter Number of Elements:'); READLN(N); FOR kounter := 1 TO N DO BEGIN WRITE('Enter Element #',kounter,': '); READLN(Number); Sum:= Sum + Number { The semicolon is optional } 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. A sample run of the program gives the following: Enter Number of Elements: 5 Enter Element #1: 1 Enter Element #2: 2 Enter Element #3: 3 Enter Element #4: 4 Enter Element #5: 5 Sum of Numbers = 15.00 Average of Numbers = 3.00 Press ENTER to continue.. Notice how the element numbers were displayed inside the loop using the values of the control variable Kounter. 4-3 Stepping Up and Stepping DownIn the previous examples, the FOR loop counter was always incremented. This means that the final value of the counter must be greater than the initial value, or else the loop will never be executed. You can decrement the counter using an alternative form of the FOR loop, by replacing the keyword TO with the keyword DOWNTO as in the following form: FOR control-variable := expression-1 DOWNTO expression-2 DO statement; With this formula you can start the counter with the larger value and step down until the final value is reached.
|
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. |