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


Example: Powers of Two

The 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:

Enter starting exponent:1
Enter ending exponent:20
Number Power of two
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
17 131072
18 262144
19 524288
20 1048576
Press ENTER to continue..

Drill 4-1

Write a program to test the leap years in the range from 1990 to 2000. Display on the screen each year and the test result as in the following output:

The year 1990 is not a leap year.
The year 1991 is not a leap year.
The year 1992 is a leap year.
The year 1993 is not a leap year.
The year 1994 is not a leap year.
The year 1995 is not a leap year.
The year 1996 is a leap year.
The year 1997 is not a leap year.
The year 1998 is not a leap year.
The year 1999 is not a leap year.
The year 2000 is a leap year.

Example: The Average

The 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 Down

In 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.


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.