JUMP STATEMENT
Jump statement are used to change the execution of prograamme sequence.
There are Four jump statement
1. Break
2. Continue
3. Go to
4. Exit
Break:-
It is to stop the loop . loop stops in two situation,
(i) when condition becomes false
(ii) when break encounter in loop
Example1:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i ;
for(i=1;i<=5;i++)
{
{
cout<<i<<endl;
break;
}
cout<<"out from loop";
getch();
}
NOTE:-
Break is basically used when we want if any unexpected situation comes then are stop the programme.
EXAMPLE 2:- Write a programme to accept marks of five subject of a student and display the total marks . if user enters invalid marks stop the loop.
#include<iostream.h.
#include<conio.h>
void main()
{
clrscr();
int i,m,s=0;
for(i=1;i<=5;i++)
{
cout<<"enter a number";
cin>>m;
if(m>100)
{
cout<<"invalid input marks cannot be>100";
break;
}
s=s+m;
}
cout<<"\n\n sum is"<<s;
getch();
}
2. Continue :- The continue statement in c++ programming works instead of forcing termination, it force the next itration (repetetion) of the loop to take place,skipping any code in between .
EXAMPLE 1:- write a programme to accept five positive number and find out the maximum.Stop the loop if user enters negative number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i=1,max=0;
while(i<=5)
{
cout<<"enter a number";
cin>>n;
if(n>0)
{
if(m>max)
max=n;
}
else
{
cout<<"the number is negative"<<endl;
break;
}
i++;
}
cout<<"maximum is\t"<<max;
getch();
}
EXAMPLE 2;- Write a programme to accept five even number and find out their sum.Stop the loop if user enters odd number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1,n,s=0;
while(i<=5)
{
cout<<"enter a number";
cin>>n;
if(n%2==0)
s=s+n;
else
{
cout<<"the number is odd"<<endl;
break;
}
i++;
}
cout<<"sum of even number"<<s;
getch();
}
EXAMPLE 3:- Write a programme to accept five character and count number of capital and number of small. Stop the loop if vuser enters other then character.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int cc=0,cs=0,i=1;
char ch;
while(i<=5)
{
cout<<"enter a number";
cin>>n;
if(ch>='A' && ch<='Z')
cc++;
else if (ch>='a' && ch<='z')
cs++;
else
{
cout<<"the input is not valid "<<endl;
break;
}
i++;
}
cout<<" no. of capital character\t"<<cc<<"\n"<<"no, of small character \t"<<cs;
getch();
}
NOTE:-
The continue doesnot work proper in loop.It's work proper in while loop.
3.Exit loop:- This function terminates the entire programme means it stops the programme.This can be used at everywhere in a programme means within loop and outside of the loop. To use exit in our programme we have include header file <process.h>
Difference between break and exit
* Break stops the loop and exit stops the programme.
* Break use only within loop buit we can use exit everywhere in the programme.
*Breal we use exit in our loop loop we must have to include its header file <process.h>.
EXAMPLE 1:- #include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
cout<<"hello";
getch();
exit(0); \\(0) or (1)
cout<<"c++ computer";
cout<<"byee byee";
}
EXAMPLE 2:- Write a programme to accept five positive number and display their sum.Stop the programme if user enters negative number.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int n,s=0;
for (int i= 1; i<=5;i++)
{
cout<<"enter a number";
cin>>n;
if(n<0)
exit(0);
s=s+n;
}
cout<<"sum of five positive number is"<<s;
getch();
}
EXAMPLE 3:- Write a programme to find out factorial of a number stops the programme if user gives negative number.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int n,f=1,i;
cout<<"enter a number ";
cin>>n;
if (n<=0)
exit(0);
for(i=1; i<=n; i++)
f=f*i;
cout<<"factoriyal is"<<f;
getch();
}
EXAMPLE 4:- Write a programme to accept all even number betwwen 1 to n stop the programme if iser enter negative number or zero for n.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int i,n;
cout<<"enter value for n";
cin>>n;
if (n<=0)
exit(0)
for(i=1;i<=m;i++)
{
if(i%2==0)
cout<<i<<endl;
}
getch();
}
0 Comments