Jump statements are used to transfer the control form one part of the program to another part.
There are three jump statements in C++ programming language.
- break statement
- continue statement
- goto statement
Break Statement in C++
This statement is used for two purpose.
- To terminate loop permanently
- To exit from switch case
Q. write a program to represent break statement.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
{
if(i==5)
break;
cout<<”\n AIPS ACADEMY”;
}
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
OR
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
{
cout<<”\n AIPS ACADEMY”;
if(i==5)
break;
}
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
Find Output
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
{
if(i==5)
break;
cout<<”\n”<<i;
}
getch();
}
output
1
2
3
4
Find Output
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
{
cout<<”\n”<<i;
if(i==5)
break;
}
getch();
}
Output
1
2
3
4
5
Continue Statement in C++
It is used to skip one execution of loop.
Q. write a program to represent continue statement.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=5; i++)
{
if(i==3)
continue;
cout<<”\n”<<i;
}
getch();
}
Output
1
2
4
5
Find output
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=5; i++)
{
cout<<”\n”<<i;
if(i==3)
continue;
}
getch();
}
Output
1
2
3
4
5
Goto Statement in C++
It is used to transfer the control from one part of the program to another part.
It can also be used for repeat some statements.
syntax
goto label;
Q. Write a program to print your name five times using goto statement.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
aips:
cout<<”\n AIPS ACADEMY”;
i++;
if(i<=5)
goto aips;
getch();
}
Output
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
AIPS ACADEMY
Q. Write a program to print numbers from 1 to 5 using goto statement.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
aips:
cout<<”\n”<<i;
i++;
if(i<=5)
goto aips;
getch();
}
Output
1
2
3
4
5