C:\oreyes\samples\java\breakcontinue>type BreakContinue.java
class BreakContinue {
public static void main( String [] args ) {
for( int i = 0 ; i < 10 ; i++ ) {
if( i % 2 == 0) { // if pair, will jump
continue; // don't go to "System.out.print" below.
}
System.out.println("The number is " + i );
if( i == 7 ) {
break; // will end the execution, 8,9 wont be processed
}
}
}
}
C:\oreyes\samples\java\breakcontinue>java BreakContinue
The number is 1
The number is 3
The number is 5
The number is 7
System.out.println ("starting loop:");
for (int n = 0; n < 7; ++n)
{
System.out.println ("in loop: " + n);
if (n == 2) {
continue;
}
System.out.println (" survived first guard");
if (n == 4) {
break;
}
System.out.println (" survived second guard");
// continue at head of loop
}
// break out of loop
System.out.println ("end of loop or exit via break");
这将导致以下输出:
starting loop:
in loop: 0
survived first guard
survived second guard
in loop: 1
survived first guard
survived second guard
in loop: 2
in loop: 3
survived first guard
survived second guard
in loop: 4
survived first guard
end of loop or exit via break
first:
for (int i = 0; i < 4; ++i)
{
second:
for (int j = 0; j < 4; ++j)
{
third:
for (int k = 0; k < 4; ++k)
{
System.out.println ("inner start: i+j+k " + (i + j + k));
if (i + j + k == 5)
continue third;
if (i + j + k == 7)
continue second;
if (i + j + k == 8)
break second;
if (i + j + k == 9)
break first;
System.out.println ("inner stop: i+j+k " + (i + j + k));
}
}
}
class BreakDemo {
public static void main(String[] args) {
int[] arrayOfInts =
{ 32, 87, 3, 589,
12, 1076, 2000,
8, 622, 127 };
int searchfor = 12;
int i;
boolean foundIt = false;
for (i = 0; i < arrayOfInts.length; i++) {
if (arrayOfInts[i] == searchfor) {
foundIt = true;
break;//this is an unlabeled break,an unlabeled break statement terminates the innermost switch,for,while,do-while statement.
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at index " + i);
} else {
System.out.println(searchfor + " not in the array");
}
}
未标记的break语句终止最里面的开关、for、while、do-while语句。
public class BreakWithLabelDemo {
public static void main(String[] args) {
search:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.println(i + " - " + j);
if (j == 3)
break search;//this is an labeled break.To notice the lab which is search.
}
}
}
带标记的break终止外部语句。如果你javac和Java这个演示,你会得到:
0 - 0
0 - 1
0 - 2
0 - 3
class ContinueDemo {
public static void main(String[] args) {
String searchMe = "peter piper picked a " + "peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;
for (int i = 0; i < max; i++) {
// interested only in p's
if (searchMe.charAt(i) != 'p')
continue;//this is an unlabeled continue.
// process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");
}
未标记的continue语句将跳过for、while、do-while语句的当前迭代。
public class ContinueWithLabelDemo {
public static void main(String[] args) {
search:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.println(i + " - " + j);
if (j == 3)
continue search;//this is an labeled continue.Notice the lab which is search
}
}
}
public static void main(String[] args) {
for(int i = 0; i < 5; i++){
if (i == 3) {
continue;
}
System.out.print(i);
}
}//prints out 0124, continue moves to the next iteration skipping printing 3
public static void main(String[] args) {
for(int i = 0; i < 5; i++){
if (i == 3) {
break;
}
System.out.print(i);
}
}//prints out 012, break moves out of the loop hence doesnt print 3 and 4
public static void main(String[] args) {
System.out.println("HelloWorld");
for (int i = 0; i < 5; i++){
System.out.println("Start For loop i = " + i);
if(i==2){
System.out.println("Inside if Statement for i = "+i);
continue;
}
System.out.println("End For loop i = " + i);
}
System.out.println("Completely out of For loop");
}
OutPut:
HelloWorld
Start For loop i = 0
End For loop i = 0
Start For loop i = 1
End For loop i = 1
Start For loop i = 2
Inside if Statement for i = 2
Start For loop i = 3
End For loop i = 3
Start For loop i = 4
End For loop i = 4
Completely out of For loop
当使用break时
public static void main(String[] args) {
System.out.println("HelloWorld");
for (int i = 0; i < 5; i++){
System.out.println("Start For loop i = " + i);
if(i==2){
System.out.println("Inside if Statement for i = "+i);
break;
}
System.out.println("End For loop i = " + i);
}
System.out.println("Completely out of For loop");
}
Output:
HelloWorld
Start For loop i = 0
End For loop i = 0
Start For loop i = 1
End For loop i = 1
Start For loop i = 2
Inside if Statement for i = 2
Completely out of For loop
System.out.println("continue when i is 2:");
for (int i = 1; i <= 3; i++) {
if (i == 2) {
System.out.print("[continue]");
continue;
}
System.out.print("[i:" + i + "]");
}
outer1:
for(int i=0; i<5; i++) {
for(int j=0; j<4; j++) {
for(int k=0; k<2; k++) {
System.out.println("[" + i + "][" + j + "][" + k + "]");
if(j == 3) {
continue outer1;
}
}
}
[0][0][0]
[0][0][1]
[0][1][0]
[0][1][1]
[0][2][0]
[0][2][1]
[0][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[1][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[1][0][1]
[1][1][0]
[1][1][1]
[1][2][0]
[1][2][1]
[1][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[2][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[2][0][1]
[2][1][0]
[2][1][1]
[2][2][0]
[2][2][1]
[2][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[3][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[3][0][1]
[3][1][0]
[3][1][1]
[3][2][0]
[3][2][1]
[3][3][0] <---- CONTINUE WITH LABEL CALLED HERE
[4][0][0] <---- CONTINUES FROM NEXT ITERATION OF OUTER LOOP
[4][0][1]
[4][1][0]
[4][1][1]
[4][2][0]
[4][2][1]
[4][3][0]