Jumping Statement In C++
Jump statements are used to alter the flow of control unconditionally.
That is, jump statements transfer the program control within a function
unconditionally. The jump statements defined in C++ are break, continue, goto
and return. In addition to these jump statements, a standard library function
exit () is used to jump out of an entire program.
1. Goto
statement
2. Break
statement
3. Continue
statement
1.
Goto statement
The C+ +
goto declaration is also called a jump declaration. The control to the other
part of the program is transferred. It saves to the specified label
unconditionally.
Syntax:
goto
label;
………….
………….
…………
label:
statement;
…………….
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int n=1;
label:
cout<<”sum of goto
is :”<<n<<endl;
n++;
if(n<=5)
goto label;
getch();
}
Fig: Goto Statement In C++ |
Output:
Sum of goto is: 1
Sum of goto is: 2
Sum of goto is: 3
Sum of goto is: 4
Sum of goto is: 5
2.
Break statement
This
loop control statement is used to terminate the loop. As soon as the break
statement is encountered from within a loop, the loop iterations stops there
and control returns from the loop immediately to the first statement after the
loop.
Syntax:
break;
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
for(int i=1;i<=5;i++)
{
if(i==3)
{
break;
}
cout<<i<<endl;
}
getch();
}
Output:
1
2
3.
Continue statement
The
declaration C++ is used for the continuation of the loop. The current program
flow continues and the remaining code is omitted at a specified state. If there
is an inner loop, only an inner loop continues.
Syntax:
jump-statement;
continue;
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
for(int i=1;i<=5;i++)
{
if(i==3)
{
continue;
}
cout<<i<<endl;
}
getch();
}
Fig: Continue Statement In C++
Output:
1
2
3
4
5
No comments:
Post a Comment