Jumping statements in C

Preview

Jumping Statements in C

There are four types of jumping statements in C language.



 1. break

break is a keyword of C language.It is used to transfer the control of program out of switch or looping statement.

Program of break statement

Output

#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
break;
printf(“\n%d”,i);
}return(0);
}
1
2

for statement starts with 1 and should go upto 5. When value of i becomes 3, break statement would execute and control would come out of for statement.


 2. continue

continue is a keyword of C language.

It is used with looping statements. It is reverse of break statement.

Program to demonstrate the use of continue statement

Output

#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
continue;
printf(“\n%d”,i);
}return(0);
}
1
2
4
5

for statement starts with 1 and should go upto 5. When value of i becomes 3, continue statement would execute and control would go to the next iteration of for statement i.e. statements after continue would be skipped and value of i would become 4 and so on.



3. goto

goto is a keyword of C language.

It is used to transfer the control of program from one part of program to another part of program. It can be used anywhere in a program.

goto statement can be used in two ways:

a. Forward goto

In forward goto statement, the label exists after the goto statement.

Program to demonstrate the use of forward goto statement

Output

#include<stdio.h>
int main()
{
printf(“\nwelcome”);
goto end;
printf(“\nHi”);      /*Line 1*/
end:                                /*Line 2*/
printf(“\nBie”);        /*Line 3*/
return(0);
}
Welcome
Bie

Description of above program

In the above program, end is the label defined by the programmer in Line2. After displaying welcome the control would transfer after the label end: i.e. Line 2 would be skipped and control would transfer after line 7.

b. Backward goto

In  backward goto statement, the label exists before the goto statement.

Program to demonstrate the use of backward goto statement

Output

#include<stdio.h>
int main()
{
int a,b,c;
char choice;
clrscr();
start:                              /*Line 1*/
printf(“\nEnter two numbers: “);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“\nSum=%d”,c);
printf(“\nWant to repeat (y/n)=”);
fflush(stdin);
scanf(“%c”,&choice);
if(choice==’y’||choice==’Y’)
goto start;                         /*Line 2*/
return(0);

}
Welcome
ByeEnter two numbers: 2
3
Sum=5
Want to repeat(y/n)=y

Enter two numbers: 5
6
Sum=11
Want to repeat(y/n)=n

In the above program, start is the label defined by the programmer in Line 1. When the program is executed for the first time, two values are entered by the user whose sum is to be found. After displaying sum of the entered values. Message would appear as “Want to repeat y/n)”. if ‘y’ is entered by the user, control would go to Line 1 and whole program  would execute again.

4. exit()

Whenever exit() function is found in a program, the program terminates and no statement after it would be executed.

Program to demonstrate the use of exit() function.

Output

#include<stdio.h>
#include<process.h>
int main()
{

int a;
clrscr();
printf(”\nEnter an integer value=”);
scanf(“%d”,&a);
if(a<0)
{
exit(0);
}
else
printf(“\nHello”;
getch();
}
Enter an integer value=0
Program terminated..

In the above program, value of a is entered by the user. If value entered by the user is negative, program would terminated because of exit(0).


Lesson tags: break statement in c, continue statement in c, exit() function in c, goto statement in c
Back to: C Programming Language