continue Statement in C++ | exit statement in C++

Here we will discuss continue statement and exit statement in C++ following are,


continue statement

In C++, continue statement is used to skip a particular statement in the program. Using this statement with another statement in C++, we can skip a particular statement in the program.

Note: continue statement does not terminate the program but during the execution only if the given expression is true, its skip a statement that means the program does not terminate.

SYNTEX:-

{
   expression;
   ……………..;
   continue;
}

Here is the Program,

#include<iostream.h>
#include<conio.h>

 void main()
  {
     clrscr();
     int x;
     
     cout<<"Enter number: ";
     cin>>x;
    
      for(int i=0; i<x; i++)
       {
         if(i==5) 
           continue;

         cout<<i<<" ";
       }

     cout<<"\nNumber 5 will be Skipped";
     getch();
  }
OUTPUT:-
Enter number: 5
0 1 2 3 4 6 7 8 
Number 5 will be Skipped

Let’s try this with another one example below,

Printing even number using continue statement

Here is the program,

#include<iostream.h>
#include<conio.h>

 void main()
 {
    clrscr();
    int n;

    cout<<"Enter finishing point: ";
    cin>>n;

     for(int i =1; i<=n; i++)
       {
         if(i%2==0)
            continue;    // skip even number

         cout<<i<<" ";
       }

    getch();
 }
OUTPUT:-
Enter finishing point: 26
1  3  5  7  9  11   13   15   17  19  21  23  25

exit() statement

to use exit() function, First we have to include its header file stdlib.h  or process.h (in which it already defined) in the program. This is a function that we use as a control statement.

This type of statement is used to terminate the whole program. with this statement, you can terminate the program within a specific condition and exit the program.

Here is the syntex to use this

SYNTEX:–

exit(int);

Here in the int data-type value which represent the status, if this value is 0 then the exit is normal, if not, the exit will be abnormal.

let’s see with an example here,

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

 void main()
  {
     clrscr();
     int x;

     cout<<"Enter number: ";
     cin>>x;
       
       if(x==0)
       {
         exit(0);
       }
      else
         cout<<"Number is: "<<x;

     getch();
}
OUTPUT:-
Enter number: 0

Enter number : 5
Number is: 5

The break type statement is used to terminate a single statement or set of statement while the continue statement is used to skip the single statement or set of statement.

The exit() function is always used to terminate an entire program while the break-statement terminates the statement only.


previous-break statement| goto Statement

next-array in C++