Examples of continue statement in c++ programing

1

the continue statement is used to skip the current iteration of the loop and the control of the program goes to the next iteration. 

All Contributions

Write a program with continue statement and nested loop :

#include <iostream>
using namespace std;
int main()
{
    int number;
    int sum = 0;
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (j == 2) {
                continue;
            }
            cout << "i = " << i << ", j = " << j << endl;
        }
    }

    return 0;
}

Example of write a program with continue statement and while loop :

#include <iostream>
using namespace std;
int main()
{
    int sum = 0;
    int number = 0;
    while (number >= 0) {
        sum += number :
        cout << "Enter a number: ";
        cin >> number;
        if (number > 50) {
            cout << "The number is greater than 50 and won't be calculated." << endl;
            number = 0;  // the value of number is made 0 again
            continue;
        }
    }
    cout << "The sum is " << sum << endl;

    return 0;
}

write a program with continue statement and for loop :

// program to print the value of i

#include <iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 5; i++) {
        // condition to continue
        if (i == 3) {
            continue;
        }
        cout << i << endl;
    }

    return 0;
}

total contributions (3)

New to examplegeek.com?

Join us