Home >Backend Development >C#.Net Tutorial >Does the continue statement 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 this loop, skip the remaining statements in the loop body, and directly enter the next loop, preparing 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 entry 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 while and for loops, and is often used together with the if conditional statement to determine whether the condition is true.
Usage
The continue statement is only used in loop statements.
In the execution of the statement set in the loop body, the continue statement is used to end this 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 extracts the numbers between 100 and 200 that are divisible by 3, and the rest can be output:
for(int n=100;n<=200;++n) { if(n%3==0) continue; cout<<n<<endl; }
Non-essential
continue statement always It is 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, of which the first part contains the continue statement:
循环体: {...} if(条件) {第一部分(包含continue语句)} {第二部分} 通过将if(){}语句改写成if-else语句,可将continue语句省略掉,并且逻辑语义不变: 循环体: {...} if(条件) {第一部分} else {第二部分}
If it is originally as follows After the above rewritten loop body structure, the continue statement is simply redundant, because the first part of the if statement is executed, and the second part belonging to 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 will get:
if(条件) {} else {第二部分} //他可以改写成: if(!条件) {第二部分}
In other words, as long as the condition is reversed, the rewriting can be completed. For example, a piece of code in the usage method can be expressed as:
for (int n=100; n<=200;++n) if(n%3!=0) cout<<n<<endl;
to 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 and second part sets described in the non-essential paragraph:
(1) should not be the structure of the parallel computing function, because the parallel structure uses if-else The statement can be described more clearly, without the need for a continue statement;
(2) The main calculation is not in the first part, because an if statement is used to frame the main calculation, and the continue statement is used to exclude the subsequent parts. In the design It's a little top-heavy.
When the loop body is executing the main calculation, use the continue statement to appropriately filter out some situations that do not meet the main calculation conditions to make the logical structure clear.
The structure of most loop bodies is not very complicated, because reasonable programming methods can appropriately avoid excessively large process bodies. If optimization and other processing make the loop body concise, using the continue statement without losing any opportunity can 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 execution of this loop body, and Instead of aborting the entire loop, the break statement ends the loop statement and no longer checks the loop condition.
Related recommendations: c language tutorial video
The above is the detailed content of Does the continue statement end the execution of the entire loop?. For more information, please follow other related articles on the PHP Chinese website!