AppDividend
Latest Code Tutorials

Continue Statement in C++ Example | C++ Continue Statement Program

0

Continue Statement in C++ Example | C++ Continue Statement Program is today’s topic. Continue works in parallel with the break statement. The only difference is that the break statement completely terminates the loop while the continue statement only skips the current iteration and goes to the next iteration. The main aim of the continue statement is to skip the execution of statements inside the loop’s body for the current iteration.

Continue Statement in C++

As the name suggests, the continue statement forces a loop to continue or execute the next iteration. 

When the continue statement is executed in a loop, the code inside a loop following the continue statement will be skipped, and the next iteration of the loop will begin.

For example, suppose we want to print numbers from 1 to 50, and we do not want to print 45 so when the loop iteration reaches use 45 we can use a continue statement that will skip the execution of that iteration and will go to the beginning of the next iteration.

Syntax of Continue Statement

continue;

See the following code snippet.

for (int i = 1; i <= 50; i++)
{
  cout << i << endl;
  if (i == 45)
  {
    continue;
  }
}

It will print all the numbers from 1 to 50 except 45.

Flow chart of Continue Statement

 

Flow chart of Continue Statement

Examples of continue statement

Continue statement inside for loop

See the following code in which we are using continue statement with for loop.

#include <iostream>
using namespace std;

int main()
{
  for (int i = 0; i <= 10; i++)
  {
    if (i == 5)
    {
      continue;
    }
    cout << "i=" << i << endl;
  }
  cout << "Missed it?\n Check 5.\n Not there?\n We used continue over there to skip it" << endl;
}

See the output.

 

Continue Statement in C++ Example

Q2- Write the program to print the table of 10 but skip the value when it becomes 90 and print the rest of the table.

#include <iostream>
using namespace std;

int main()
{
  int k;
  for (int i = 0; i <= 10; i++)
  {
    if (k == 80)
    {
      k = 0;
      continue;
    }
    k = 0;
    k = i * 10;
    cout << "10 x " << i << "=" << k << endl;
  }
  cout << "I have skipped 10 x 9 = 90" << endl;
}

See the output.

 

C++ Continue Statement Program

Use of continue statement in While loop

See the following program of continue statement with the while loop.

#include <iostream>
using namespace std;

int main()
{
  int e = 5;
  while (e >= 0)
  {
    if (e == 4)
    {
      e--;
      continue;
    }
    cout << "Value of e: " << e << endl;
    e--;
  }
  return 0;
}

See the following output.

Value of e: 5
Value of e: 3
Value of e: 2
Value of e: 1
Value of e: 0

Use of continue in do-While loop

See the following program of continue statement with the do-while loop.

#include <iostream>
using namespace std;

int main()
{
  int j = 4;
  do
  {
    if (j == 7)
    {
      j++;
      continue;
    }
    cout << "j is: " << j << endl;
    j++;
  } while (j < 10);
  return 0;
}

See the following output.

j is: 4
j is: 5
j is: 6
j is: 8
j is: 9

C++ Continue Statement with Inner Loop

C++ Continue Statement continues the inner loop only if you use continue statement inside the inner loop. See the following code.

#include <iostream>
using namespace std;

int main()
{
  for (int i = 1; i <= 3; i++)
  {
    for (int j = 1; j <= 3; j++)
    {
      if (i == 2 && j == 2)
      {
        continue;
      }
      cout << i << " " << j << "\n";
    }
  }
}

See the output.

1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

Finally, Continue Statement Tutorial With Example is over.

Recommended Posts

C++ Switch Example

If-else Statements in C++ Example Tutorial

Recursion Program In C++ Tutorial

C++ Access Specifiers

Exception Handling In C++

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.