Tech Study

continue statement in c++

Introduction :

Continue statement in c++ can be considered to be a loop that controls the statement that is supposed to force the program control for executing the next iteration from the loop. Talking about the results, the code that is inside the loop that follows the continue statement is going to get skipped and the next iteration of the loop is going to begin.

Syntax:

continue;

Example : Considering any situation where we might need to write some program that is supposed to be printing numbers that will be from 1 to 10 but except 4. It can be specified that one will have to do this with the help of loop and in this case only one loop is allowed to be used. In such cases comes the usage of the continue statement. We are supposed to do here is that we will have to run a loop from 1 to 10 and everytime we will be having to compare the value of the iterator with the value 4. If in some case it becomes equal to 4 then we have to use continue statement right there for continuing to the next iteration and otherwise we are supposed to be printing the values simply.

Continue statements with for loops:-

While using continue statements with for loops we will have to put this statement inside the loops so that the control flow jumps to the next iteration without executing the current iteration completely.

#include <iostream>

using namespace std;

  

int main()

{

      for (int ri = 1; ri <= 10; ri++) {

  

        // If ri becomes equal to 4,

        //we have to jump to next iteration

        // without executing

        if (ri == 4)

            continue;

  

        else

            // otherwise we have to print the value of ri

            cout << ri << " ";

    }

  

    return 0;

}

continue statement in c++

Output:

1 2 3 5 6 7 8 9 10 

Continue statements with while loops:-

Similar to using the continue statements with the for loops we are also allowed to be using the continue statements with the while loops. It is supposed to skip the current iterations and jumps the control flow to the next iteration.

#include <iostream>

using namespace std;

  

int main()

{

    int ri = 0;

     while (ri < 10) {

          ri++;

         if (ri == 4) {

            continue;

        }

  

        else {

                      cout <<ri << " ";

        }

    }

  

    return 0;

}

Output:

1 2 3 5 6 7 8 9 10 

Continue statements with do-while loops:-

The do-while loop differs from other loops as it enters the loops without any prior condition checking and executes the code written inside.Continue statements however works similarly with respect to the for and while loops. They jump the control flow to the next iteration.

#include <iostream>

using namespace std;

  

int main()

{

  

    int ri = 0;

       do {

  

            ri++;

  

        if (ri == 4) {

            continue;

        }

        else {

                       cout << ri << ' ';

        }

  

    } while (ri < 10);

    return 0;

}

Output:

1 2 3 5 6 7 8 9 10

Continue statement with nested loops:-

Not differing much from the above techniques the continue statements in this case are also supposed to be jumping the control flow to the next iteration.

#include <iostream>

using namespace std;

  

int main()

{

  

    for (int i = 1; i <= 2; i++) {

        for (int j = 0; j <= 4; j++) {

            if (j == 2) {

                continue;

            }

            cout << j << " ";

        }

        cout << endl;

    }

  

    return 0;

}

continue statement in c++

Output:

0 1 3 4 

0 1 3 4 

 

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more