1. Home > programming >

Is the function of the continue statement to end the execution of the entire loop?

Wrong, the function of the continue statement is not to end the execution of the entire loop, but to end the loop, skip the remaining statements in the loop body, and directly enter the next loop, ready to execute the loop body again.

The function of the continue statement is to end this loop, skip the remaining statements in the loop body and force it into the next loop (return to the beginning of the loop body to prepare Execute the loop body again). The continue statement is only used in the while and for loops, and is often used together with the if conditional statement to determine whether the condition is true.

How to use

The continue statement is only in the loop statement.

In the execution of the statement set in the loop body, the continue statement is used to end the loop. In the for loop, jump to the execution of the loop step statement to prepare to test the conditions of the next loop; in the while loop, jump directly to the loop condition test. For example, the following code puts the number between 100 and 200 that is divisible by 3, and the rest can be output:

for(int n=100;  n<=200;++n) {if(n%3==0) continue; cout<


The continue statement is always executed conditionally.

In the loop body, when the statement is executed to the conditional statement containing the continue statement, the following statement set can be divided into two parts, the first part contains the continue statement:

Loop body: {...} if(condition) {the first part (including the continue statement)} {the second part} By rewriting the if(){} statement into an if-else statement, you can  The continue statement is omitted, and the logical semantics remain unchanged: Loop body: {...} if (condition) {first part} else {second part}

If it was originally the loop after rewriting above Body structure, then the continue statement is simply redundant, because the first part of the if statement is executed, and the second part of the else is directly skipped through the if structure.

If there is only one continue statement in the first part, after omitting the continue statement, you get:

if (condition) {} else  {Part 2} //It can be rewritten as: if(!Conditions) {Part 2}

In other words, as long as the conditions are reversed, the rewriting can be completed. For example, a piece of code in the usage mode can be expressed as:

for (int n=100; n<=200;++n) if(n%  3!=0) cout<

Get the code without the continue statement. Therefore, the continue statement in the loop is not necessary.

The loop body describes the calculation process. Where continue is used, for the first part and second part set described in the non-essential paragraph:

(1) It should not be a structure of parallel calculation function, because the parallel structure uses if- The else statement can be described more clearly, without the continue statement;

(2) The main calculation is also not in the first part, because an if statement is used to frame the main calculation and a continue statement is used to exclude the subsequent parts. A little top-heavy in design.

When the loop body is in the process of executing the main calculation, use the continue statement to properly filter some situations that do not meet the main calculation conditions, which can make the logical structure clear.

The structure of most loop bodies is not very complicated, because the use of reasonable programming methods can appropriately avoid the process body from being too large. If optimization and other processing make the loop body concise, use the continue statement without losing the opportunity to make the structure clearer.

The difference between continue and break

The difference between the continue statement and the break statement is:

The continue statement only ends the loop body Instead of suspending the entire loop, the break statement ends the loop statement and no longer performs loop condition judgment.

Content recommendation: free high-definition PNG material download

This article is published by www.internetweblist.com and does not represent the position of www.internetweblist.com/:http://www.internetweblist.com/programming/55055.html

Contact Us

Online consultation:click here to give a message