Home  >  Article  >  类库下载  >  The impact of break and continue in C++ loops on functions

The impact of break and continue in C++ loops on functions

little bottle
little bottleOriginal
2019-04-02 13:08:013538browse

Some auxiliary functions are sometimes used in loop statements, including break and continue. The break statement exits the loop and no longer executes the remaining statements of the loop. The continue statement ends the current loop and returns to the beginning of the loop to start a new cycle.

The impact of break and continue in C++ loops on functions

The function of the break statement is to stop the currently executing loop (for, while, do while) or switch multi-branch program structure, and do the following of these structural functions Content. In the switch statement, break is used to cause the process to jump out of the switch statement and continue executing the statement after the switch.
In loop statements, break is used to jump out of the nearest closed loop body.

For example, after executing break, the following code continues to execute the statement at "a=x 2;" instead of jumping out of all loops:

for (x=0;x<5;x++ ){ 
for (i=0;i<5;i++ ){if (i==1)break;}
a=x+2; //break跳至此处
...}

continue statement The function is to end the currently executing loop (for, while, do...while), and then execute the next loop. That is, the statements in the loop body that have not yet been executed are skipped, and then a judgment is made whether to execute the loop next time.

In the for loop, continue is used to execute the next loop.
In while loops and do...while loops, continue is used to execute the judgment of conditional expressions.

For example: Output an odd number between 1 and 100.

for (int i=0; i<=100; i++){
   if (i%2==0)  continue;    //当i被7整除时,执行continue语句,结束本次循环,即跳过cout语句,转去判断i<=100是否成立。只有i不能被7整除时,才执行cout函数,输出i。
   cout << i << endl;
}

To sum up, the difference between the continue statement and the break statement is that the continue statement only ends the current loop, rather than terminating the execution of the entire loop. The break statement ends this loop and no longer performs conditional judgment.

[Recommended course: C Video Tutorial]

The above are the details of break and continue in the C loop statement. For more information, please pay attention to other related articles on the PHP Chinese website!


The above is the detailed content of The impact of break and continue in C++ loops on functions. 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
Previous article:what is c#Next article:what is c#