Simple comparison of continue and break statements in C++

青灯夜游
Release: 2019-02-19 10:41:16
Original
2817 people have browsed it

"break" and "continue" are both "jump" statements, specifically used to change the normal flow of the program. They can transfer control of the program to another part of the program. So what's the difference between them? The following article will introduce you to the continue and break statements, and introduce the difference between continue and break statements. I hope it will be helpful to you.

Simple comparison of continue and break statements in C++

break statement

The break statement terminates the smallest closed loop (i.e. while, do-while, for or switch statement).

In C, break has only two uses, namely, first, it is used to "terminate the execution of the case in the switch statement"; second, "terminate the loop and restore control to the next statement after the loop" , break only lets the program exit the loop surrounding it. If break is used with nested loops, it will only break the innermost loop without affecting the outer loop.

Simple comparison of continue and break statements in C++

continue statement

The continue statement skips the rest of the loop statement and causes the next time in the loop Iterations occur. It stops execution of the remaining code in the current iteration of the loop and restores control to the next iteration of the loop; it skips the current iteration of code and passes control to the next iteration of the loop.

Simple comparison of continue and break statements in C++

The difference between continue and break statements

Let’s use examples to introduce the difference between continue and break statements. The difference between

#include  using namespace std; main() { int i; cout << "在循环中使用break语句: \n"; for (i = 1; i <= 5; i++) { // 当 i 为3的倍数时会脱离循环 if ((i % 3) == 0) break; else cout << i << " "; } cout << "\n在循环中使用continue语句:\n"; for (i = 1; i <= 5; i++) { // 在1~5中输出不是3倍数的数 if ((i % 3) == 0) continue; cout << i << " "; } }
Copy after login

Program description:

1. In the first for loop, here we use the break statement.

● When the loop iterates for the first time, i = 1; the if statement evaluates to false, so the else statement is executed.

● The loop iterates again, now i = 2; the calculation result of the if statement is false, and the else statement is executed.

● The loop iterates again, now i = 3; if the condition result is true, the following break is executed, and the loop is interrupted.

2. In the second for loop, here we use the continue statement.

● When the loop iterates for the first time, i = 1; the if statement evaluates to false, so the else conditional statement is executed.

● The loop iterates again, i = 2; the calculation result of the if statement is false, and the else statement is executed.

● The loop iterates again, i = 3; if the condition evaluates to true, the code here stops in the middle and starts a new iteration until the end condition is met.

Output:

Simple comparison of continue and break statements in C++

##Main difference: The break statement terminates the remaining iterations of the loop, Let the control exit the loop; the continue statement only terminates the current iteration of the loop, the control still continues the loop, and it lets the control enter the next iteration of the loop.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of Simple comparison of continue and break statements in C++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!