Jumping statements- Switch,continue,break and goto

What are jumping statements?
C language provides us multiple statements through which we can transfer the control anywhere in the program.

There are basically 3 Jumping statements:
1. break jumping statements.
2. continue  jumping statements.
3. goto jumping statements.

1. break jumping statements.

·         By using this jumping statement, we can terminate the further execution of the program and transfer the control to the end of any immediate loop.
·         To do all this we have to specify a break jumping statements whenever we want to terminate from the loop.

Syntax: break;

NOTE: This jumping statements always used with the control structure like switch case, while, do while, for loop etc.

NOTE: As break jumping statements ends/terminate loop of one level . so it is refered to use return or goto  jumping statements , during more deeply nested loops.

Example: Program based upon break jumping statements:

WAP to display the following output:
1          2          3          4          5          .           .           .           .           .
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
for(i=1;i<=10;i++)
{
printf(“Enter ”,i,”no”);
printf(“ \t  %d ”,i);
if(i>5)
break;
}
}

2. continue jumping statements.


·         By using this jumping statement, we can terminate the further execution of the program and transfer the control to the begining of any immediate loop.
·         To do all this we have to specify a continue jumping statements whenever we want to terminate terminate any particular condition and restart/continue our execution.

Syntax: continue;

NOTE: This jumping statements always used with the control structure like switch case, while, do while, for loop etc.

Example: Program based upon continue jumping statements:

WAP to display the following output:
1          2          3          4          .           6          7          .           9          10


#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
for(i=1;i<=10;i++)
{
printf(“Enter ”,i,”no”);
printf(“ \t  %d ”,i);
if(i==5 || i==8)
continue;
}
}


3. goto jumping statements.

·         By using this jumping statements we can transfer the control from current location to anywhere in the program.
·         To do all this we have to specify  a label with goto and the controlwill transfer to the location where the label is specified.

Syntax: goto <label>;

NOTE:
·         The control will transfer to those label that are part of particular function, where goto is specified.
·         All those labels will not included, that are not the part of a particular function where the goto is specified.

NOTE:
·          It is good programming style to use the break, continue and return instead of goto.
·         However, the break may execute from single loop and goto executes from more deeper loops.

Example: Program based upon continue jumping statements:

WAP to display the square root of a no, if no. is not positive then re enter the input.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
float s;
start:
printf(“Enter a no.”);
scanf(“%d”,&n);
s=sqrt(n);
if(n<=0)
goto start;
printf(“%f”,s);
}

Previous Page                                                                                                                                Next Page

Spread the word! By sharing this Post!

Pageviews This Week