Java Continue Statement: Usage and Examples

In Java, the continue statement is used to skip the remaining code in the current loop and move on to the next iteration. When the continue statement is executed, the program will immediately jump to the next iteration of the loop and start executing the next loop. The continue statement is usually used in conjunction with an if statement to skip the remaining code in the loop under specific conditions.

Here are examples of how to use the continue statement:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue; // 如果i是偶数,则跳过当前循环
    }
    System.out.println(i);
}

In the above example, when i is an even number, the continue statement will skip the System.out.println(i) code and directly move to the next iteration. Therefore, the System.out.println(i) code will only be executed when i is an odd number.

bannerAds