This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

preface

The confusion of Java beginners deserves everyone’s attention.

We like to tell others what is what, in fact, we should tell him how to learn what is what, that is, it is better to teach someone to fish than to take fish from others.

Let’s go into this study.

Problem of repetition

Today we are going to talk about the difference between the use of the Break and continue keywords in JAVA learning.

Problem solving

The whole problem is actually quite simple, but it can be confusing for beginners to know when to use break and when to use continue. Let’s, uh, take a closer look at the usage scenario.

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");
Copy the code

First of all, both are used in logical statements, to control statements, to control jumps.

Break, directly breaks the entire code block, or if in a loop, breaks the loop and continues with the rest of the method

Continue, interrupts this loop and jumps to the next loop judgment logic

The above is the general usage

conclusion

Time is a problem, in the process of learning, must be careful analysis, can not rush.