Break and Continue

 

Break: We already discussed about one of the use of this statment earlier.The break statement can also be used to jump out of a loop.

 

#include <iostream>

using namespace std;

int main() 
{
  for (int i = 0; i =< 10; i++) {
    if (i = = 6)  {
      break;
    }
    cout << i << "\n";
  } 

  return 0;
}

 

Output

0
1
2
3
4
5

 

Continue: The continue statement breaks one time repetition of the loop if a specified condition occurs and continues with the next iteration in the loop.

 

Example

 

#include <iostream>

using namespace std;

int main() 
{
  for (int i = 0; i =< 10; i++) {
  if (i == 6) {
    continue;
  }
  cout << i << "\n";
  } 

  return 0;

}

 

Output

0
1
2
3
4
5
7
8
9
10