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


Drill 4-5

Rewrite the last program using an inner FOR loop and an outer WHILE loop.

As you can see in the form, this loop is ready to execute more than one statement without using the BEGIN-END blocks. Another difference between the WHILE loop and the REPEAT loop is that the REPEAT loop is executed at least once, regardless of the condition, because it starts each round by executing the statements and ends by testing the condition. In some applications this feature is necessary.

Look at the factorial algorithm using a REPEAT loop:

       ...
    Factorial := 1;
    Kounter := Number;
   REPEAT
    Factorial := Factorial * Kounter;
    Kounter := Kounter - 1;
   UNTIL Kounter = 0;

When the “Kounter” reaches zero (which means that the value “1” was already used up), no other rounds are needed, and the loop is terminated. You may also use the stepping-up algorithm, thus:

    ...
    Factorial := 1;
    Kounter := 1;
    REPEAT
     Factorial := Factorial * Kounter;
     Kounter := Kounter + 1;
    UNTIL Kounter = Number + 1;

In this case the loop is terminated when the value of “Kounter” reaches “Number+1,” which means that the value “Number” was already used up.

In the following program this REPEAT loop is nested in a WHILE loop. The program will be repeatedly executed until you enter 0 to terminate it.

{ ------------------------------ figure 4-9 ------------------------------ }
PROGRAM FactorialProg2(INPUT,OUTPUT);
VAR
 Factorial   :REAL;
 Kounter, Number :INTEGER;
BEGIN
 WRITE('Give me a number (or 0 to exit): ');
 READLN(Number);
 WHILE Number <> 0 DO            { Start of the WHILE loop }
  BEGIN
   Factorial := 1;
   Kounter := 1;
   REPEAT                    { Start of the REPEAT loop }
    Factorial := Factorial * Kounter;
    Kounter := Kounter + 1;
   UNTIL Kounter = Number + 1;         { End of the REPEAT loop }
   WRITELN('The factorial of ', Number,' is ', Factorial:0:0);
   WRITE('Give me a number (or 0 to exit): ');
   READLN(Number)
  END;                       { End of the WHILE loop }
 WRITELN('I am out of here!')
END.

Notice here that two similar input statements are used, one before the WHILE loop and one inside it. The first one is used to initialize the variable “Kounter” before entering the loop, in order to be ready for testing within the loop.

A sample run of the program gives the following:

Give me a number (or 0 to exit): 3
The factorial of 3 is 6
Give me a number (or 0 to exit): 5
The factorial of 5 is 120
Give me a number (or 0 to exit): 0
I am out of here!

Summary

In this chapter you were introduced to three control structures used to build loops. These structures are:

  The FOR loop
  The WHILE loop
  The REPEAT loop
1.  The FOR is used to repeat a statement or a block of statements a specified number of times. The loop takes the general form:
    FOR control-variable := expression-1 TO expression-2 DO
         statement;

where:
control-variable is the loop counter,
expression-1 is the initial value, and
expression-2 is the final value.
2.  An alternate form of the FOR loop is used to decrement the counter:
    FOR control-variable := expression-1
                       DOWNTO expression-2 DO
         statement;
3.  The WHILE loop is used to execute a statement or a block of statements as long as a specified condition is TRUE. The construct takes the general form:
   WHILE condition DO
       statement;
4.  With both the FOR and the WHILE loops you can use multiple statements by including them in a BEGIN-END block.
5.  The REPEAT loop is used to execute a group of statements until the specified condition fails. It takes the general form:
    REPEAT
        statement-1;
        statement-2;
        ...
        statement-n;
    UNTIL condition;
6.  You understand now that the main difference between the REPEAT loop and the other two is that the statements inside the REPEAT loop are executed at least once regardless of the condition.
7.  You understand also that the REPEAT loop can handle many statements without using BEGIN-END blocks.
8.  Finally, you learned in this chapter that loop constructs may be nested inside other constructs (including other loops).


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.