Using break as a Form of Go to
In addition to its uses with the switch statement and loops, the break statement can
also be employed by itself to provide a "civilized" form of the go to statement. Java
does not have a go to statement, because it provides a way to branch in an arbitrary and unstructured manner. This usually makes goto-rldden code hard to understand and hard to maintain. It also prohibits certain compiler optimizations. There are, however, a few places where the go to is a valuable and legitimate construct for flow control. For example, the go to can be useful when you Me exiting from a deeplynestec , set of loops.To handle such situations, Java defines an expanded form of the break statene not, By .using this form of break, you can break out of one or more blacks of code. These blocks need not be part o".a loop or a switch. They can be any block-Further, you can specify precisely where execution will resume, because this form of break works with a la bel. As you will see, brink gives you the benefits of a goto without its problems. the general form of the labeled break statement is shown here: Here, label is the name of a label that identifies a block of code. When this form of b real,executes, control is transferred oUt of the named block of code. The labeled block of code must enclose the break statement, but it does not need to be the immediately you us oil labeled break statement to exit from iI set of nested blocks. you cannot use break to transfer control to a block of code th at does not enclose the break statement.
To mime a block, put a label at the start of it. A label is any valid Java identifier followed by a colon. Once you have labeled a block, you can then use·this label as the target of a break statement. Doing so causes execution to resume at the end of the labeled block.For example, the following program shows three nested blocks, each with its own label. The break statement causes execution to jump forward, past the end of the block labeled second, skipping the two statements.
Sometimes it is useful to force an early iteration of a loop. That is, you want to continue running the loop, but stop processing the remainder of the code in its body for this particular Iteration, This is, in effect, a go to just past the body of the loop. to the loop's end. The continue statement performs such an action. In while and do-while loops, 0 continue statement causes control to be transferred directly to the conditional . expression that controls the loop. In a for loop, control goes first to the iteration portion