The execution of a program is sequential but we can change this sequential manner by using jump statements. The jump statements are
1. goto statement:
By using goto we can transfer the control anywhere in the program without any condition. The syntax is goto label;
Example:
#include<iostream>
using namespace std;
int main()
{
float a,b;
cout<<"Enter 2 numbers";
cin>>a>>b;
if(b==0)
goto end;
cout<<"The quotient is "<<a/b;
return 0;
end:cout<<"Division by zero error";
}
2. break statement:
It is used to skip over a part of the code i.e. we can premature exit from a loop such as while, do-while, for or switch.
Syntax:
while (expression)
{
if (condition)
break;
}
Eg.
#include<iostream>
using namespace std;
main()
{
int i=1;
while(i<10)
{
cout<<i<<endl;
if(i==5)
break;
i++;
}
}
The output is
1
2
3
4
5
3. continue statement:
It bypasses one iteration of the loop.
Syntax:
while (expression)
{
if (condition)
break;
}
Eg.
#include<iostream>
using namespace std;
main()
{
int i=0;
while(i<10)
{
i++;
if(i==5)continue;
cout<<i<<endl;
}
}
The output is
1
2
3
4
5
6
7
8
9
10
4. exit(0) function:
It is used to terminate the program. For this the header file cstdlib must be included.