continue | exit statement

In this page we will discuss about two-

 

continue statement

This statement is used to skip a particular statement in the program. Using this statement with another statement, you can skip a particular statement in the program.

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

SYNTEX:-

{
   expression;
   ……………..;
   continue;
}
#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 it, 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, if this value is 0 then the exit is normal, if not, the exit will be abnormal

let’s see with an example

#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 | goto Statement

next-array and their types