26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

222 Control Structures: Part 2 Chapter 5<br />

Fig. Fig. 5.13 5.13 Using a labeled break statement in a nested for structure (part 2 of 2).<br />

The continue statement proceeds with the next iteration (repetition) of the immediately<br />

enclosing while, for or do/while structure. The labeled continue statement,<br />

when executed in a repetition structure (while, for or do/while), skips the remaining<br />

statements in that structure’s body and any number of enclosing repetition structures and<br />

proceeds with the next iteration of the enclosing labeled repetition structure (i.e., a repetition<br />

structure preceded by a label). In labeled while and do/while structures, the program<br />

evaluates the loop-continuation test immediately after the continue statement<br />

executes. In a labeled for structure, the increment expression is executed, and then the<br />

loop-continuation test is evaluated. Figure 5.14 uses the labeled continue statement in a<br />

nested for structure <strong>to</strong> enable execution <strong>to</strong> continue with the next iteration of the outer<br />

for structure.<br />

The labeled for structure (lines 14–32) starts at the nextRow label. When the if<br />

structure at line 24 in the inner for structure detects that column is greater than row, the<br />

continue statement at line 25 executes, and program control continues with the increment<br />

of the control variable of the outer for loop. Even though the inner for structure<br />

counts from 1 <strong>to</strong> 10, the number of * characters output on a row never exceeds the value of<br />

row.<br />

Performance Tip 5.4<br />

The program in Fig. 5.14 can be made simpler and more efficient by replacing the condition<br />

in the for structure at line 21 with column , = and

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!