Break Notes:-
/*There are, however,
a few places where the goto is a valuable and legitimate construct for flow
control. For
example, the goto can be useful when you are exiting from a deeply nested set of
loops.
To handle such situations, Java defines an expanded form of the break statement.
By
using this form of break, you can break out of one or more blocks of code. These
blocks
need not be part of 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
label.
As you will see, break gives you the benefits of a goto without its problems.
The general form of the labeled break statement is shown here:
break label;
Here, label is the name of a label that identifies a block of code. When this
form of break
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
enclosing block. This means that you can use a labeled break statement to exit
from a
set of nested blocks. But you cannot use break to transfer control to a block of
code that
does not enclose the break statement.
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*/
Example 1:-
// Using break as a civilized form of goto.
class Break {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
/*There are, however,
a few places where the goto is a valuable and legitimate construct for flow
control. For
example, the goto can be useful when you are exiting from a deeply nested set of
loops.
To handle such situations, Java defines an expanded form of the break statement.
By
using this form of break, you can break out of one or more blocks of code. These
blocks
need not be part of 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
label.
As you will see, break gives you the benefits of a goto without its problems.
The general form of the labeled break statement is shown here:
break label;
Here, label is the name of a label that identifies a block of code. When this
form of break
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
enclosing block. This means that you can use a labeled break statement to exit
from a
set of nested blocks. But you cannot use break to transfer control to a block of
code that
does not enclose the break statement.
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*/
Example 1:-
// Using break as a civilized form of goto.
class Break {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}