Home  >  Article  >  Java  >  What is the function of continue statement in loop statement?

What is the function of continue statement in loop statement?

王林
王林Original
2020-07-03 13:49:0929060browse

The function of the continue statement in the loop statement is: 1. End this loop, and subsequent statements in the loop body will not be executed; 2. Continue to judge the loop conditions and execute the next loop body statement. The continue statement can only be used in a while statement, for statement or foreach statement.

What is the function of continue statement in loop statement?

(Recommended learning: java introductory program)

Function:

  • End this loop, the subsequent statements in the loop body will not be executed

  • Continue to judge the loop conditions and execute the next statement in the loop body

Detailed introduction:

The continue statement is similar to the break statement, but it can only appear in the loop body. The difference between

and break statement:

continue does not interrupt the loop statement, but terminates the current iteration of the loop and enters the next iteration. Simply put, continue ignores the current loop of the loop statement.

Note: The continue statement can only be used in the loop body of a while statement, a for statement, or a foreach statement. Using it anywhere else will cause a syntax error.

There are two ways to use the continue statement in the loop body, with or without labels. The syntax format is as follows:

continue //不带标签
continue label //带标签,label是标签名

(Video tutorial recommendation: java video tutorial)

Code example:

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 0; i < numbers.length; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println("Count is: " + i);
}

In the above program code , when the condition i==3 is executed, the continue statement will terminate this loop. The statements after continue in the loop body will no longer be executed, and the next loop will continue, so there is no 3 in the output result.

The program running results are as follows:

Count is: 0
Count is: 1
Count is: 2
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9

The above is the detailed content of What is the function of continue statement in loop statement?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn