In Java , The break statement has 3 functions.
First, in a switch statement, it is used to terminate a sequence of statements.
Second, it can be used to exit a loop. (When a series of loops are nested, just jump out of the inside layer)
Thirdly, it can be used as a " advanced " goto statement.
The last 2 usages are explained below.
Class Breakloop {
public static void Main (String args[]) {
for (int i=0; i<100; i++) {
if (i = = ten) break; Terminate loop if I is 10
System.out.println ("I:" + i);
}
System.out.println ("Loop complete.");
}
}
Results:
i:0
I:1
I:2
I:3
I:4
I:5
I:6
I:7
I:8
I:9
Loop complete.
Class BreakLoop3 {
public static void Main (String args[]) {
for (int i=0; i<3; i++) {
System.out.print ("Pass" + i + ":");
for (int j=0; j<100; J + +) {
if (j = = ten) break; Terminate Loop if J is 10
System.out.print (j + "");
}
System.out.println ();
}
System.out.println ("Loops complete.");
}
}
Results:
Pass 0:0 1 2 3 4 5 6 7 8 9
Pass 1:0 1 2 3 4 5 6 7 8 9
Pass 2:0 1 2 3 4 5 6 7 8 9
Loops complete.
As you can see, the break statement in the inner loop only terminates the loop, and the outer loop is unaffected.
For break, here are two points to remember.
First, you can have more than one break statement in a loop.
Second, break in the switch statement only affects the switch statement, without affecting any of the loops.
The general format of the label break statement is as follows:
Break label;
Label labels are labels that identify code blocks. When this form of break executes, the control is passed out of the specified code quickly. Tagged code must enclose the break statement.
To specify a block of code, add a label at the beginning of it. A label can be any legally valid Java identifier followed by a colon. Once you add a tag to a block, you can use the tag as the object of the break statement. Doing so causes the execution to restart at the end of the tagged block class break {public static void main (String
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 off of second blockSystem.out.println ("This won ' t execute");
}//third
System.out.println ("This won ' t execute");
}//second
System.out.println ("This was after second block.");
}//first
}
}
Results:
Before the break.
This was after second block.
One of the most common uses of the label break statement is to exit the loop nesting. For example, in the following program, the outer loop executes only once:
Class BREAKLOOP4 {
public static void Main (String args[]) {
outer:for (int i=0; i<3; i++) {
System.out.print ("Pass" + i + ":");
for (int j=0; j<100; J + +) {
if (j = = ten) break outer; Exit both loops
System.out.print (j + "");
}
System.out.println ("This is not print");
}
System.out.println ("Loops complete.");
}
}
Results:
Pass 0:0 1 2 3 4 5 6 7 8 9 Loops complete.
Remember that if a tag is not defined in a block that surrounds break, you cannot break to that label. For example, the following program is illegal and will not be compiled:
Class Breakerr {
public static void Main (String args[]) {
one:for (int i=0; i<3; i++) {
System.out.print ("Pass" + i + ":");
}
for (int j=0; j<100; J + +) {
if (j = = ten) break one; Wrong
System.out.print (j + "");
}
}
}
Because a loop labeled one does not enclose the break statement, control cannot be passed to that block.
Parsing of Java break