Jumping Statements:
Break
Statement :
Sometimes, it is necessary to exit immediately from a loop as
soon as the condition is satisfied.
When break statement is used inside a loop, then it can cause to
terminate from a loop. The statements after break statement are skipped.
Syntax :
break;
Figure :
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for(i=1; ; i++)
{
if(i>5)
break;
printf("%d",i); // 5 times only
}
getch();
}
Output :
12345_
Continue Statement :
Sometimes,
it is required to skip a part of a body of loop under specific conditions. So,
C supports 'continue' statement to overcome this anomaly.
The
working structure of 'continue' is similar as that of that break statement but
difference is that it cannot terminate the loop. It causes the loop to be
continued with next iteration after skipping statements in between. Continue
statement simply skipps statements and continues next iteration.
Syntax :
continue;
Figure :
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=10; i++)
{
if(i==6)
continue;
printf("\n\t %d",i); // 6 is omitted
}
getch();
}
Output :
1
2
3
4
5
7
8
9
10_
Goto Statement :
It is
a well known as 'jumping statement.' It is primarily used to transfer the
control of execution to any place in a program. It is useful to provide
branching within a loop.
When
the loops are deeply nested at that if an error occurs then it is difficult to
get exited from such loops. Simple break statement cannot work here properly.
In this situations, goto statement is used.
Syntax :
goto [expr];
Figure :
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1, j;
clrscr();
while(i<=3)
{
for(j=1; j<=3; j++)
{
printf(" * ");
if(j==2)
goto stop;
}
i = i + 1;
}
stop:
printf("\n\n Exited !");
getch();
}
Output :
* *
Exited_
No comments:
Post a Comment