Learn C++ - C++ break and continue

The break and continue statements enable a program to skip over parts of the code.

You can use the break statement in a switch statement and in any of the loops.

It causes program execution to pass to the next statement following the switch or the loop.

The continue statement is used in loops and causes a program to skip the rest of the body of the loop and then start a new loop cycle.


The following code shows how the two statements work.

#include <iostream>
using namespace std;
const int my_size = 80;
int main(){//from ww  w .j ava 2s .co  m
    char line[my_size];
    int spaces = 0; 

    cout << "Enter a line of text:\n";
    cin.get(line, my_size);
    cout << "Complete line:\n" << line << endl;
    for (int i = 0; line[i] != '\0'; i++)
        cout << line[i];    // display character
        if (line[i] == '.') // quit if it's a period
        if (line[i] != ' ') // skip rest of loop
    cout << "\n" << spaces << " spaces\n";
    return 0;

The code above generates the following result.