Of course, it is not always easy to write all of your for, while and do…whileloops so that they are easy to read and yet the loops terminate on exactly the right pass through the loop. Java makes it easier to jump out of loops and to control other areas of program flow with its break and continuestatements.
The break Statement
Earlier in this chapter, you saw how the breakstatement is used to exit a switchstatement. In a similar manner, breakcan be used to exit a loop.
As an example of this, consider the following code:
int year = 1909;while (DidCubsWinTheWorldSeries(year) == false) {System.out.println("Didn't win in " + year);if (year >= 3000) {System.out.println("Time to give up. Go White Sox!");break;}}System.out.println("Loop exited on year " + year);
This example shows a whileloop that will continue to execute until it finds a year that the Chicago Cubs won the World Series. Because they haven't won since 1908 and the loop counter yearstarts with 1909, it has a lot of looping to do. For each year they didn't win, a message is displayed. However, even die-hard Cubs fans will eventually give up and change allegiances to the Chicago White Sox. In this example, if the year is 3000 or later, a message is displayed and then a breakis encountered. The breakstatement will cause program control to move to the first statement after the end of the whileloop. In this case, that will be the following line:
System.out.println("Loop exited on year " + year);
The continue Statement
Just as a break statement can be used to move program control to immediately after the end of a loop, the continue statement can be used to force program control back to the top of a loop.
Suppose you want to write a method that will count and display the number of times the Cubs have won the World Series this century. One way to do this would be to first see if the Cubs played in the World Series and then see if they won. This could be done as follows:
int timesWon = 0;for (int year=1900; year <= 2000; year++) {if (DidCubsPlayInWorldSeries(year) = false)continue;if (DidCubsWinWorldSeries(year)) {System.out.println("Cubbies won in " + year + "!");timesWon++;}}System.out.println("The Cubs won " + timesWon + " times.");
In this case, a for loop is used to iterate through the years from 1900 to 2000. The first line within the loop tests to see if the Cubs played in the World Series. If they didn't, the continuestatement is executed. This moves program control back to the for loop. At that point, year is incremented and the expression year <= 2000is retested. If year is less than or equal to 2000, the loop continues. If, however, DidCubsPlayInWorldSeriesequals true, then the continuestatement is skipped, and the next test is performed to see if the Cubs won that year.
Using Labels
Java does not include a gotostatement. However, the fact that gotois a reserved word indicates that it may be added in a future version. Instead of goto, Java allows you to combine breakand continue with a label. This has an effect similar to a gotoin that it allows a program to reposition control. In order to understand the use of labels with breakand continue, consider the following example:
public void paint(Graphics g) {int line=1;outsideLoop:for(int out=0; out<3; out++) {g.drawString("out = " + out, 5, line * 20);line++;for(int inner=0;inner < 5; inner++) {double randNum = Math.random();g.drawString(Double.toString(randNum), 15, line * 20);line++;if (randNum < .10) {g.drawString("break to outsideLoop", 25, line * 20);line++;break outsideLoop;}if (randNum < .60) {g.drawString("continue to outsideLoop", 25, line * 20);line++;continue outsideLoop;}}}g.drawString("all done", 50, line * 20);}
This example includes two loops. The first loops on the variable out, and the second loops on the variable inner. The outer loop has been labeled by the following line:
outsideLoop:
This statement will serve as a placeholder and as a name for the outer loop. A random number between 0 and 1 is generated for each iteration through the inner loop. This number is displayed on the screen. If the random number is less than 0.10, the statement break outsideLoop is executed. A normal break statement in this position would break out of the inner loop. However, since this is a labeled break statement, it has the effect of breaking out of the loop identified by the name. In this case, program control passes to the line that displays "all done" since that is the first line after outsideLoop.
On the other hand, if the random number is not less than 0.10, the number is compared to 0.60. If it is less than this, the statement continue outsideLoop is executed. A normal, unlabeled continuestatement at this point would have the effect of transferring program control back to the top of the inner loop. Because this is a labeled continue statement, program control is transferred to the start of the named loop.
No comments:
Post a Comment