For a nested loop break and continue

                                     break和continue

1.continue usage

Let me talk about a simple for loop continue usage,

for (int i=0; i<3; i++){
    if(i==1){
        continue;
    }
       System.out.println(i)
}

The above results: 02. Shows continue directly to the i == 1 cycle skipped.

Continue to look for nested under:

for(int j=0; j<3; j++){
    for (int i=0; i<3; i++){
        if(i==1){
            continue;
        }
           
           System.out.print("i:"+i)
    }
    System.out.println("j:"+j)
}

Operation result is i: 0 i: 2 j: 0,

                      i:0  i:2 j:1

                      i:0  i:2 j:2

If the tape label would happen then? We look

a://a在此处运行结果为:i:0 i:0 i:0
for(int j=0; j<3; j++){
    a://a在此处,结果与无a标记一样
    for (int i=0; i<3; i++){
        if(i==1){
            continue a;
        }
           
           System.out.print("i:"+i)
    }
}

2.break usage

Let's look at the use of a simple for loop

for (int i=0; i<3; i++){
    if(i==1){
        break;
    }
       System.out.println(i)
}
//运行结果为0

Know break, would direct the end of the cycle, the cycle is interrupted.

Let's look at the use of nested for loop

for(int j=0; j<3; j++){
    for (int i=0; i<3; i++){
        if(i==1){
            break;
        }
           
           System.out.print("i:"+i)
    }
    System.out.println("j:"+j)
}
/*运行结果为i:0 j:0
           i:0 j:1
           i:0 j:2

Can know for nested loops, break direct the inner loop termination,

Take a look at the for loop with labels

a://a在此处,运行结果为:i:0
for(int j=0; j<3; j++){
    a://a在此处,结果与无a标记一样
    for (int i=0; i<3; i++){
        if(i==1){
            break a;
        }
           
           System.out.print("i:"+i)
    }
    System.out.println("j:"+j)
}

These are my two days to see all the data usage summary of break and continue, there may be insufficient, hope brightest Great God he said.

Published 14 original articles · won praise 8 · views 4745

Guess you like

Origin blog.csdn.net/qq_41223538/article/details/81531071