Jumping statements in C

Preview

Jumping Statements in C

Jumping statements are used to transfer the control of program from one point to another within the program. There are three types of jumping statements provided by C language.

  • break
  • continue
  • goto
  • exit

 1. break

break is a keyword of C language. It is used with switch and looping statements.

It is used to transfer the control of program out of switch or looping statement.

In switch statement it is written after each and every case so that only one case should execute at a time.

When we write break statement in a looping statement, all the statements after it are skipped and program control leaves the looping statement.

In looping statement, this statement is usually associated with some condition for exiting the loop.

Syntax of break is:

break;

Program to demonstrate the use 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

Description of above program

In the above program, 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.

It is used to transfer the control of looping statement to next iteration of loop.

When we write continue statement within a loop, all the statements written after keyword continue are skipped and the control goes to next iteration of loop where Iteration is the process of repetition of loop.

This statement is usually associated with some condition for continuing with the next iteration of loop.

The syntax of continue statement is:

continue;

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

Description of above program

In the above program, 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.

This statement is usually associated with some condition for transferring the control of program from one part of program to another part.

This statement should be used very carefully as it may result in infinite loop. So this statement should be avoided if possible.

goto statement is always used with a label which is any valid identifier of C language.

Syntax of goto statement is:

goto Label;

goto is a keyword.

Label is any valid identifier of C language. It is defined by the programmer depending upon his own requirement. This label must appear somewhere else in the same program. The label defined must be ended with a colon.

goto statement can be used in two ways:

a. Forward goto

In forward goto statement, the label exists after the goto statement. The syntax of forward goto statement is:

goto Label;

:

:

Label:

Program to demonstrate the use of forward goto statement

Output

#include<stdio.h>
int main()
{
printf(“\nwelcome”);
goto end;
printf(“\nHello”);      /*Line 6*/
end:                                /*Line 7*/
printf(“\nBye”);        /*Line 8*/
return(0);
}
Welcome
Bye

Description of above program

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

b. Backward goto

In  backward goto statement, the label exists before the goto statement. The syntax of backward goto statement is:

Label:

:

:

:

goto Label;

Program to demonstrate the use of backward goto statement

Output

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

}
Welcome
ByeEnter two values: 2
3
Sum=5
Do you want to repeat(y/n)=yEnter two values: 5
6
Sum=11
Do you want to repeat(y/n)=n

Description of above program

In the above program, start is the label defined by the programmer in Line 7. 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 “Do you want to repeat y/n)”.

if ‘y’ is entered by the user, control would go to Line 7 and whole program  would execute again. The message asking for repeating the program again appears.

On typing ‘n’, the program terminates.

4. exit()

It is a predefined function in C language. Whenever this function is found in a program, the program terminates and no statement after it would be executed.

The syntax of this function is:

exit(Termination);

Termination is argument of integer type. It can take zero as well as non-zero value.

If the value of termination is zero, it means that the program terminates normally.

If it contains a non-zero value, it means that the program terminates abnormally. To use this function in our program, we need to use header file process.h.

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)
{
printf(”\nProgram terminated..”);
getch();
exit(0);
}
else
printf(“\nWelcome”;
getch();
}
Enter an integer value=0
Program terminated..

Description of above program

In the above program, value of a is entered by the user. If value entered by the user is 0, the message “Program terminated..” would be displayed and program would terminated because of exit(0).


DIFFERENCE BETWEEN BREAK AND CONTINUE

BREAKCONTINUE
1. break is a jumping statement1. continue is a jumping statement.

2. It can be used with switch as well as looping statements.

 

2. It can be used only with looping statement.
3. This statement takes the program control out of the switch or looping statement where it is used.3. This statement takes the program control to the next iteration of the looping statement.

4. Syntax:

break;

4. Syntax:

Continue;

 

Tips and tricks 

  • goto statement should be carefully used as it may result in infinite loop.
  • exit() is a predefined function to exit the program.
  • break is a jumping statement which can be used with switch as well as looping statements.
  • continue is a jumping statement which can be used only inside looping statements.

 

Test Your Knowledge

Write the output of the following code:

1.

int a=10,b=5;
while(b<=a)
{
printf ( “\n%d “,b) ;
if(b>3)
break;
++b;
}

2.
int i=10;
do
{
printf ( “\n%d “,b) ;
if(i%3==0)
continue;
i+=5;
}while(i<=50);

3.
int i,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
if(i==2)
break;
}

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