Do It Again
You have thus far studied most of what it takes to make a program run. You have learned about using methods, and, a little at a time, you have been learning how to create the methods that take care of the work for you. The last foundational part of making a program work is learning to make certain parts of the program repeat themselves.
The last of the programming fundamentals you will need to learn about is program repetition, otherwise known as looping. Like decision-making, program repetition allows a computer to modify its actions to help resolve a given problem at hand. By itself, repetition is fairly easy to do. The real issues are related to when you should repeat a process, or when you should not, and if you do, how many times, or under what circumstances, you should repeat the process. Like decision-making processes, your skills will be required to develop appropriate logic and operations that will make these actions come to pass correctly.
The two formats in the Java programming language available for repetition are simple looping, and recursion. In this chapter, you will primarily be working with looping, but you will be exposed to recursion for your future reference. You will find that either repetition format is easy to use once you understand managing the iterative process. Having done this, you will be ready to tackle the data structure studies that you will find in the future.
Implementing Repeated Operations
Computers have two talents that make them really valuable for problem solving. The first is their ability to change their actions based on differing conditions, which is why you learned about branching in the previous topic. The second is the computer's ability to iterate, or repeat its actions. Anyone can punch numbers into a calculator to get some mathematical results without a computer, but it is too difficult for anyone, or sometimes any group of people to accurately and efficiently punch in several thousand or several million calculations when that is required of the given process. The ability to repeat problem solving processes will support much greater flexibility for your programming projects.
As mentioned in previous chapters, you will be a far better programmer if you use the structured top-down programming process and write your program in pseudo code before you attempt to write it in the programming language. Program repetition requires this more than most other programming operations. You must be very clear as to exactly what conditions need to be met to start the loop, and then what conditions are required to end the loop.
Consider the following loop condition, written in pseudo code.
// prompt the user for the number of cash entries that will be made
// get the number of cash entries
// if there is more than one cash entry, begin repetition
The conditions for looping in this case are simple. If more than one cash entry has been accepted, begin a repeated process.
To make a loop work, you need to consider three basic things:
- some initializing condition(s): preparation of variables or conditions that will help the loop get started, and likely be involved with its continuation
- some condition(s) to continue: one or more operations or tests that help the loop decide if it should continue iterating. Note that there are some programming languages where there might be a condition to cease operation of the loop; the Java programming language does not have this kind of loop test, but you should be advised that it may exist
- some updating action(s): in order for the loop to make an intelligent or logical decision to continue iterating, some variables or operations will be updated or changed on a regular basis, usually related to the repetition. After some number of iterations, these variables or operations will change state in such a way as to cause the condition to continue to become false, and cause the loop to cease iterating
Like many other parts of your experience thus far, the processes may look very difficult or complicated. However, following simple analyzing and testing steps like the ones provided here make the process of iteration very easy. It is not much more than a "Stop and Think" moment that will save you many hours of stress trying to figure out when to start, when to end, and what to do. This brief introduction to iteration will get you started into the next topics with which you will be working.