An article to thoroughly understand the use of the continue and break keywords in Java (code included)

Explanation

In Java, we often have to traverse the data and process
the data . In the data processing, we often use two keywords, one is continue and the other is break. The meaning of the
continue keyword is: end the current loop and continue the next loop . The meaning of the
break keyword is: end the loop .


Not much to say directly on the code

continue

public class Demo2 {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
                System.out.println("此处i==3:--->跳过此次循环,继续下次循环!");
                continue;
            }
            System.out.println("i = " + i);
        }
    }
}

The above code is very simple, a for loop from 0-5,
when i == 3, we jump out of the loop, that is, the following code is not executed, and continue to the next loop.

operation result:

i = 0
i = 1
i = 2
此处i==3:--->跳过此次循环,继续下次循环!
i = 4


我们可以看到i = 3没有打印 


break

public class Demo3 {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
                System.out.println("此处i==3:--->结束循环!");
                break;
            }
            System.out.println("i = " + i);
        }
    }
}

The above result is also a for loop of 0-5. When i == 3, we use break to modify and end the loop!

operation result

i = 0
i = 1
i = 2
此处i==3:--->结束循环!

很明显,程序进行到i == 3 时就停止了。

The above is the most basic use of continue and break, and friends should use it according to the specific situation in the project.

We also have a situation in the project: traversal needs to be nested, that is, there is also a for loop in the for loop. Let us study this situation together:

public class Demo4 {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                if (i == j) {
                    continue;
                }
                System.out.println("i = " + i + ":j = " + j);
            }
        }
    }
}

Two for loops, the outer for loop is from 0-3, the inner for loop is from 0-5, when i == j,
we end this loop and continue to the next loop, but at this time the end is the inner Layer or outer layer? ? ?
Little friends can think about it first and then look at the results:

i = 0:j = 1
i = 0:j = 2
i = 0:j = 3
i = 1:j = 0
i = 1:j = 2
i = 1:j = 3
i = 2:j = 0
i = 2:j = 1
i = 2:j = 3

从运行结果中我们可以清楚的看出,结束的是内层循环,就近原则嘛!
但是如果实际情况需要,当i == j时我们就想让外层询还结束怎么办?



此时我们可以给外层循环起一个名字,然后使用continue的时候在他后面跟上这个名字,代码如下:
public class Demo4 {
    public static void main(String[] args) {
        outF:
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                if (i == j) {
                    continue outF;
                }
                System.out.println("i = " + i + ":j = " + j);
            }
        }
    }
}

The meaning here is to tell the computer: when i == j, I want to end the outer loop and continue the next loop
.

i = 1:j = 0
i = 2:j = 0
i = 2:j = 1


The operation of break is similar to continue, and it also supports the method of aliasing. Friends can try it by themselves!

Published 4 original articles · praised 4 · visits 103

Guess you like

Origin blog.csdn.net/qq_40585384/article/details/105444106