Section 10e

Reflecting on Loops


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Concept(s) of Repetition

You have looked at the three primary operations necessary for a successful loop. Some kind of initializing condition is necessary, some kind of test for continue, and some kind of updating process are necessary. However, there is an additional analysis that can help you decide what goes in your loop, as well as what does not go in your loop.

The analysis is easy. Ask the following two questions:

Think about the loops you have studied in this section, and consider these analyses:

These are fairly simple loops used for getting you started with the concepts. However, as your loops become more complex, you must conduct this analysis so that you execute only the actions that need executing, and your loops respond appropriately to the changes occurring with each iteration. One of the first things you will want to know about is how to discuss the natural variability of looping.

The concept of N

You observed the letter N used in the post-test loop example. For another example of the use of N, a person might be designing a grading program for N students. In either case, this means the program must be flexible enough to work for any number of menu items or students, and most programs are. There are also times where a loop might iterate, or repeat, "N times". Again, the loop must be prepared to loop some variable number of times, and the programmer must prepare the program to handle N iterations. Just keep in mind that N is simply used as a variable that means "some number of things".

The following is an example demonstrating how the use of N will tighten up the pseudo code on the menu loop. If you know that there are N menu items, you should be able to specify that there will be N menu choices. This means that you can get more specific in your decision making statements. Consider the following.

// start loop here
{
// display menu choice 1

// display menu choice 2

.

.

// display menu choice N

// prompt for user input

// accept user input

// check user choice less than 1 or greater than N
{
// display an error message

// tell user to try again

// pause the program so that user can read this message
}
}
// end loop, stop on correct answer
// while user choice is less than 1 or greater than N

Note here that the condition to continue is the failure condition that the user did not enter the correct number. Later, you will see this slightly refined so that it will only leave the loop for a specific input quantity; however, this is still kind of a backwards logic in the sense that you will continue until you have a specific reason to stop the loop, or end the repeated action. You will have a chance to learn more about this later. Finally, one obvious observation is that these statements can be made even more concise by using mathematical symbols. You will also see this done later in this reference, but for right now, the important thing is to be able to read and understand the pseudo code for these loop conditions. At a point in the near future, you will be implementing them yourself.

Two kinds of loops

You have now studied the two kinds of loops that Java (along with most other languages) supports . They are the pre-test kind of loop, and the post-test kind of loop. You have only looked at the syntax in a video, but as mentioned before, you will not be surprised when you see how each works. In the Java programming language, there are two ways to implement the pre-test loop, and one way to implement the post-test loop. As stated previously, as long as you get the concepts down, the syntax of these loops is trivial.

The most important thing about this topic is to "practicing safe looping". It is very easy to sit down with a loop idea in your mind and type it into your compiler. It is even easier than that to get the loop wrong and cause a condition called an infinite loop, which, just as it sounds, is a loop that does not stop. The cursor on the console screen just winks at you (in a judgemental laughing kind of way).

Remember that the pre-test loops conduct a test before they start; for example, it is possible for one of these loops to never go through any iterations if it fails the test at the beginning.

On the other hand, the post-test loops always go through one iteration before they are tested. Make sure you are clear on these specifications, and you will always be able to select the proper loop for your circumstance.

If you want your loops to work the first time and every time, you will write them in pseudo code first, and then you will translate them to the Java language, which as you will soon see, is amazingly easy. The rule for loops is identically the same as the rule for the rest of your programs. "If you cannot write it in English, or your native language, you do not understand what it is doing." This goes especially for loops because it is more embarrassing when your loop locks up your computer than when some other little bug just keeps the program from working correctly.

The Process of Repetition

The process of repetition is simply repeating some segments of code over and over. Your job is to identify the things you want repeated, identify the initialization condition(s), specify the condition(s) to continue, and conduct the updating action(s).

In addition, conducting the simple "What is supposed to happen?" and "What is supposed to change?" analysis before you write the repetition code will save you long periods of chasing down bugs that lock up your program . . . or don't do anything at all. And you always want your program to do something.