Hello, I’m Liang Tang.

This is part 27 of the EasyC++ series on break and continue statements

If you want a better reading experience, you can visit the Github repository.

Break and continue

Break and continue are both commonly used statements in the body of a loop, and not only in C++ but also in other languages. The body of the loop is greatly enriched by the presence of break and continue, both of which are used to skip parts of the code, but the details of execution are different and the usage scenarios are different.

break

Break means to end the loop. When the program runs to break, it will jump out of the loop body and execute the rest of the loop body. Can be used in any loop (for, while, and do while).

Such as:

int a[5] = {1.6.3.10.8};

for (int i = 0; i < 5; ++i) {
    if (a[i] >= 10) {
        break;
    }
    cout << a[i] << "";
}
Copy the code

The output on the screen would be 1, 6, and 3, because a[I] = 10 breaks out of the for loop. The part of the loop body that follows the break statement is also not executed.

In addition, break can only break out of one loop, if we use multiple loop nesting, break can only break out of the current loop, not the entire loop body.

for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
        if (condition) {
            break; }}// todo
}
Copy the code

In the example above, we execute a break in the J loop, which immediately breaks out of the J loop, but the outer I loop continues execution.

So what do we do if we want to get out of multiple loops?

There are probably two ways to do this. One is to add an exit flag and execute the break multiple times manually.

bool flag = false;
for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
        if (condition) {
            flag = true;
            break; }}if (flag) break;
}
Copy the code

We created a variable of type bool to indicate whether we want to break out of the loop altogether. We then add this variable to the outer loop and execute another manual exit if flag is true.

Of course, we can also combine it with the judgment condition in the loop body and write it like this:

bool flag = false;
for (int i = 0; i < n && ! flag; ++i) {for (int j = 0; j < m; ++j) {
        if (condition) {
            flag = true;
            break; }}}Copy the code

There are several other variants, all of which are not very different from each other.

Another way is to use the goto statement:

for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
        if (condition) {
            goto outloop;
        }
    }
}

outloop: cout << "end" << endl;
Copy the code

However, goTO statements are notoriously dangerous and can cause many unexpected problems, so don’t use goTO statements unless you have to. This is for reference only.

continue

The continue statement also skips the code after the statement, but does not exit the loop, instead entering the next loop.

int a[5] = {1.6.3.10.8};

for (int i = 0; i < 5; ++i) {
    if (a[i] >= 10) {
        continue;
    }
    cout << a[i] << "";
}
Copy the code

The result of the above code is 1, 6, 3, 8, with a[I] == 10 in the middle. The continue is executed, skipping all logic after the body of the loop and entering the next loop.

Because our code logic is so simple, the same logic could be used without the continue statement:

int a[5] = {1.6.3.10.8};

for (int i = 0; i < 5; ++i) {
    if (a[i] < 10) {
        cout << a[i] << ""; }}Copy the code

However, if the code in the loop body is very complex, using continue can improve the readability of the code compared to using a giant if statement. This requires a case-by-case analysis based on the actual situation, and there is no standard answer.