next up previous
Next: whileing Away Time: Some Up: Loops Previous: Loops

Programming while, controlling flow

A little while ago, we started talking about flow control statements: namely, we talked about the if statement. The if statement is useful if we want to test some condition and execute one set of statements if it's true and a different set of statements if it's false.

Now consider the following scenario: we are registering students, and we want a piece of software that allows us to register a long line of students. How long? Don't know. Let's say we did know, and the number is 1000. Then given what we've learned so far, we'd have to write pretty much the same bits of code 1000 times: create a new Student object, fill in the member variables, and so on. 1000 times. This isn't very convenient. Added to which we don't actually know how many students are going to register. What if we write the code 1000 times, and 1001 students show up to register? This isn't a very good state of affairs.

What we want is a Java statement that allows us to execute the same set of statements over and over again -- to be able to force the flow of control to go over and over the same statements. In Java we can do this with the while statement.

The syntax is:


while (expr) {

\\ statements to be repeated go here

}

Recall that the portion of the program between the brackets { and } is called a block. Therefore, the preceding construction executes the block of statements over and over. Well, what it really does is to test to see whether expr is true, execute the statements in the block, and then test to see whether expr is true again and so on while expr is true. When expr evaluates to false, the flow of control drops to the statements following the while block.

A schematic looks like this:

Figure 20: Program flow around/through while statement
\begin{figure}
\begin{center}
\epsfxsize =5in {\epsfbox {whilestatement.ps}} \\
\end{center}\end{figure}

The dotted line in Figure 20 means that control jumps over the statements; none of the statements next to the dotted curve are executed. Therefore, if expr is false when we encounter the while statement the first time, then none of the statements in the block are executed.

The flow control diagram on the left side (when expr is true) shows why this construction is called a loop. Control continues to execute the block, 'going around in circles', if you will. If/when expr finally evaluates to false, then control jumps over the block and continues with the rest of the program.

Most of the time, we want to change the value of expr within the block. If its value isn't changed by statements within the block, then only one of two things can happen: either expr is true, in which case we loop forever, or expr is false in which case we never execute any of the statements within the block. The first case is one which we sometimes use, but the second isn't. In fact most of the time we don't want to loop forever either, so we provide some way for the value of expr to change within the loop.

Can you figure out a way to a) initialize some boolean expression appropriately b) create a loop which counts the number of times it has been executed and prints it, and c) asks the user if s/he wants to terminate the loop on each pass through it? Somehow you have to take the input of the user and modify the value of expr... But how???


next up previous
Next: whileing Away Time: Some Up: Loops Previous: Loops
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09