Simple Iteration
Making Spaces
Consider the problem of printing spaces in a program. Perhaps you wish to center a title on the screen. You have to find out the number of characters in the title, the number of characters available (e.g., the width of the screen), and you have to make a calculation as to where to place the first letter of the title so that your title will be printed in the center.
In the following condition, the title is thirty characters wide, and the width of this page is found to be seventy-two characters. With a little math, you can find that you must print twenty-one spaces first, and then print the title, and it will be centered. The centered title is shown below.
<- 21 spaces ->Oops, I Shot the Piano Player!
Now your job is to create a segment of code that makes these spaces. You could make a method that prints 21 spaces, called print21Spaces, but you know that you are going to have to make several code methods like this to print all the possible numbers of spaces from zero up to thirty or more. That would be a lot of work, and consume a great deal of program space.
So, you choose to create a code segment. It will be able to print any number of spaces from zero up. For the title operation, you might limit the number to half the screen size (i.e., thirty-six in this case), but you can work with that kind of limitation later. For the moment, think about what needs to happen when you write a method that prints any specified number of spaces.
- initializing condition: the number of spaces is specified
- condition to continue: not all of the spaces have been printed
- updating action: each time a space is printed, decrement the number still to be printed
Consider the following pseudo code.
if there are more than zero spaces specified to be output
begin the loop
print a space
subtract one from the number of spaces to be printed
if the number of spaces left to print is 1 or greater
repeat the loop
if the number of spaces left to print is less than 1
end the loop
The above example is a nice place to start, but it needs some refinement. As you recall from your work with branching, if the branch does not happen, you do not have to tell the program what to do. It just goes on to the next line of code. This is the same with loops, so that last line is unnecessary. This makes the process a little smaller, as shown below.
if there are more than zero spaces specified to be output
begin the loop
print a space
subtract one from the number of spaces to be printed
if the number of spaces left to print is 1 or greater
repeat the loop
Next, if you look closely at the two decision making statements (e.g., at the beginning and at the end), you will notice that they are basically the same. So, to refine the process a little more, you could just combine the two statements at the beginning of the loop, as shown below; a note is added at the end of the loop to remind you that when the program steps down to that point, the program process will be directed back up to the "more than zero spaces" test.
if there are more than zero spaces specified to be output
begin the loop
print a space
subtract one from the number of spaces to be printed
go back to the beginning of the loop, and test for permission to run again
The next refinement will just help remove some of the text from the process. While different languages use different characters, the Java programming language uses curly braces to identify the beginning and end of loop code blocks, just as they do the beginning and end of if statements.
The beginning of the loop block will be an open curly brace, and the end of the loop block will be a closing curly brace. You will have to remember that when a program gets to the end of the loop code (i.e., the closing curly brace), it will know to return to the beginning of the loop. Here is the product of our next improvement.
// check for more than zero spaces to be output
{
// print a space
// decrement number of spaces to be printed
}
// end loop
Now this would look pretty good, but it would be awfully easy to confuse your loops with your if statements given the above pseudo code. You need to use terminology that will more clearly relate to looping instead of simple branching. Consider the following.
// loop until zero spaces remain
{
// print a space
// decrement number of spaces to be printed
}
// end loop
Now you can see what would happen to your loop in some normal segment of code
// other code
// get the number of spaces (initializing operation)
// loop until zero spaces remain
{
//print a space
// decrement number of spaces to be printed
}
// end loop
// other code
This brief topic was an introduction to thinking about loops or iteration. The actual program code for most loops is trivial. However, the thought behind them has to be effective and appropriate. This is where you need to apply your best problem-solving and thinking skills.