duperouzel.com

| Home | Part 1 | Part 2 | Part 3 |

Chapter 3 - More Program Structure

Contents:

  1. More Loops
  2. The Repeat Loop
  3. The While Loop
  4. Chapter 3 Exercises

More Loops


The For Loop and Conditional Loops

The for loop executes a predetermined number of iterations which is defined by the values in <first> and <last>. Most loops do not have that predetermined nature. Their number of iterations may depend upon other things, such as the number of entries in a file or the number of times a player wishes to play a computer game. These are called conditional loops, where a test is performed during each iteration to determine whether to carry on, or jump out of the loop to the next part of the program.

 


The Repeat loop


The simplest of the conditional loops is the repeat.until loop. This syntax will literally repeat it's instructions until a certain condition is met:

<statement>;
repeat
  Write( 'Do you want to stop yet? (Type "y" or "n")> ');
  ReadLn( Response )
until (Response = "n") or (Response = "N");
<statement>;
    

The above loop will continue to execute until the user types upper or lower case n.

Syntax

The repeat loop has a very simple syntax. Because the repeat and until statements form their own bounds to the structure, there is no need to use begin and end as is usually the case. The <condition> at the end must follow the usual conditions for Boolean evaluation in just the same way as the conditions in the if statement seen previously.

{ Single statement Repeat...Until }

<statement>;
repeat
  <statement>
until <condition>;
<statement>;

{ Multiple statement Repeat...Until }

<statement>;
repeat
  <statement 1>;
  <.
             .>;
  <statement n>
until <condition>;
<statement>;
    

Because the condition is at the end of the loop, this construct is used when the loop will always need to be executed at least once. The program must execute the lines of the repeat loop at least once in order to reach the condition at the end.

 


The While Loop


The while loop is also conditional, but this time the condition is at the beginning. The while loop is used in conditions where the loop may never need to be executed. If the condition is not met, the loop is skipped entirely.

<statement>;
while not EOF(DataFile) do
  begin
    ReadLn( DataFile, Number );
    WriteLn( Number );
    Total := Total + Number
  end; { while }
<statement>;
    

The code above checks whether it is at the end of the input file DataFile before reading the next line. When the file is empty the loop is skipped and the next statement is executed. If the file was already empty, or already at the end, the loop would be skipped entirely.

Syntax

{ Single statement while loop }

<statement>;
while <condition> do
  <statement>;
<statement>;

{ Multiple statement while loop }

<statement>;
while <condition> do
  begin
    <statement 1>;
    <.
               .>;
    <statement n>
  end; { while }
<statement>;
    

Multiple statements are bounded by the usual begin and end, unlike the repeat loop above.

 


Chapter 3 Exercises


Validation Routine

Validation is a consideration in almost all programs. Design a program which prompts for and reads an integer number. To be valid, the number must be between 0 and 999 inclusive. If the number is outside the range, the program must respond to this and prompt for the number again (as many times as necessary). The validated number must then be echoed back to the user before termination.

Code and test your design for the above problem. Which numbers did you use for testing and why?

Solution


Keeping Count

Design a program to read a list of valid integer numbers from the user. The numbers must be between 0 and 999 as in the program above (hint, hint!). The program should count the numbers and keep a running total as the they are entered. The program should then terminate when a 'rogue value' of 0 is entered. It is important to test your algorithm before you implement your code. What happens if the user was to type 0 as the first number? Does your design deal with this appropriately?

Again - code and test your design for the above solution. Which numbers did you use for testing and why?

Solution


Compound Interest

In the last worksheet, you wrote a routine to calculate the appreciation of a capital sum for a given interest rate over a specified number of years. Redesign the routine to do the opposite. The user must provide the program with an initial amount of capital, and the annual (fixed) rate of interest and a target amount. The program must calculate (and output) the interest and new capital sum for each year, until the capital exceeds the target. The program must produce a summary line which confirms the initial amount, the interest rate, the final amount and how many years the investment took.

Solution


Ulam's Conjecture

The conjecture is as follows:

  1. Start with any positive integer.
  2. If the number is even, then divide by two.
  3. If the number is odd, multiply by 3 then add 1.

This sequence is repeated until (as the conjecture states) the sequence ends at the number 1 - regardless of the initial figure - Try it! Design & write a program to test the conjecture

Solution


© 1997-2001 Dave Duperouzel.

You are free to print these pages for teaching and learning purposes, provided they are printed in entirety and all acknowledgements to the author are left intact.

Design & Implementation by Dave Duperouzel @ duperouzel.com

duperouzel.com