The Iteration Control Structure (DOWHILE)In this lesson, you will
|
The while statement has a block of code (from line 4 through line 8) that is structurally
similar to the if statement as it has a conditional expression for testing and a block
of code enclosed in a pair of braces { }. However, it works in a slightly different way. Here's how:
|
| A good loop construct depends on a proper coordination among three factors. A loop construct must have a unique variable to control the logic flow. We shall call it the control variable. In Program 4-1, the control variable is $count. The first factor is that the control variable must have an initial stage ($count starts with 1.) The second factor is that the control variable must have an ending stage (when $count exceeds 5.) The ending stage is clearly stated in the conditional expression. The third factor is that there should be a way for $count to incrementally advance from the initial stage to the ending stage. (Line 7 serves this purpose.) |
|
Once the program is running correctly, it can be made to count to 10 with very little effort.
All we have to do is change the 5 in the conditional expression to 10 (see Line 4 below). We can make it
to count to 1000, 5000, or a million just as easily.
|
Saying hello 5 times | |||
|
Program 4.2. Saying hello 5 times. The hello program in Lesson 1 says hello once per each program run. Once you learned how to loop, it is easy to make it say more. Instead of the counted numbers 1, 2, 3, 4, and 5, we will have "Hello!" in their places.
| |||
Repeating tasks | |||
|
Some of the computer functions are very repetitious. A loop is perfect for such a situation.
The following is a program that draws 10 asterisks (*) in 6 lines to make a square.
Program 4.3. Drawing a square. This program introduced a new string operator, the times operator x. It is a lowercase x letter. It will repeat the string before it with the number of times indicated by the number that follows it. So "*" x 10 means "**********". and " Hello! " x 5 means five Hello! in a row. The result of the above program displays the 6 x 10 square:
| |||
Looping with Variations | |||
There are situations when a slight bit of variation to a repetitious task is needed. For
example, if we want to draw a hollow square with four sides of asterisks and notice that
the top and the bottom sides of the square are different from the lines in the middle,
we can loop to draw only those in the middle and take care of the top and bottom sides
separately in the following program.
Program 4.4. The hollow square. The Squre2.pl program displays a hollow square. | |||
More Uses of the Control Variable | |||
|
The line count variable $line varies from 2 to 5 or 1 to 6 as shown in the previous two
programs. This variable is useful when we want to draw positional varying shapes like a
triangle.
Program 4.5. The triangle program. The result of the above program displays the triangle: While $line is 1, the print statement in the loop draws one asterisk. When it is 2, it draws 2 asterisks, and so on. The line number assists in determining how many asterisks are needed. | |||
The User-determined Triangle | |||
|
The above program will display a triangle of a fixed size. The next program displays a
triangle of a variable size depending on the user's request. It asks the height of the
triangle the user wishes to draw and then proceeds to draw the requested size.
Program 4.6. The User-determined Triangle. A possible interaction between the user and the program is shown below: The user keyed in 3 when prompted. A 3-line triangle is drawn. | |||
Adding More Numbers | |||
|
The adding program, Program 2.6 of Lesson 2, adds two input numbers and displays
the sum. With loops, we can have the user enter many more numbers with very little additional
code. The following program adds five numbers.
Program 4.7. Adding more numbers. This program initializes the variable $sum to zero and the loop control variable $count to 1. When it goes through the steps of a loop, it tests for the $count value to be within 5, then it accepts a number, adds it to $sum, adds one to $count, then jumps back to test for the next loop. It exits the loop when the $count exceeds 5. The statement following the block prints the sum accumulated in $sum. The sum depends on the numbers entered. One possible user interaction is shown below:
| |||
Using A Sentinel Value | |||
|
It is possible now to modify the program to accept 10, or 100 numbers with very little
effort. What if we will let the user decide when enough is enough? The following
program does just that. When the user finishes inputting all the numbers, enter a # sign
to signal the end. A sign which serves to signal the end is called a sentinel value.
Program 4.8 Controlling loop with a sentinel value. The program is more complicated because the code for accepting the first number is separated from that for accepting the subsequent numbers. This is necessary to cover the rare occasion in which a user shall decide that the first number is #. In that case, the while statement tests the condition ($inputNumber != "#") to be false. It skips over the loop body and executes the last statement which prints the sum as zero. The following are possible interactions with the user when running the program:
| |||
Lesson 4 Exercises | |
| |
| M. Mark, 2002.03.18 | | Introduction | Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4 | Appendix | |