Next Continue in C++

Find this useful? Support us: Star on GitHub 6
Category: Loop | Language: C++

next and continue are control statements in C++ used to skip over a particular iteration in a loop. The main difference between them is that next will skip the current iteration and continue with a new iteration while continue will skip the current iteration and resume with the next iteration.

Here's an example of next being used in a for loop:

for(int i=1; i<=10; i++) {
    if(i == 5)
        continue;
    cout << i << " ";
}

This loop will iterate through the values 1 to 10, but when i is equal to 5, the continue statement will skip that iteration and move on to the next iteration. The output of this loop will be:

1 2 3 4 6 7 8 9 10

Here's an example of continue being used in a while loop:

int i = 0;
while(i < 10) {
    i++;
    if(i % 2 == 0)
        continue;
    cout << i << " ";
}

This loop will iterate through the values 1 to 10, but when i is even, the continue statement will skip that iteration and move on to the next iteration. The output of this loop will be:

1 3 5 7 9