The goto statement in C Language

The goto statement is used to alter the normal sequence of program instructions by transferring the control to some other portion of the program.

Syntax for The goto statement

goto label;

Here, label is an identifier that is used to label the statement to which control will be transferred. The targeted statement must be preceded by the unique label followed by the colon.

label : statement;

Although goto statement is used to alter the normal sequence of program execution but its usage in the program should be avoided. The most common applications are:

i). To branch around statements under certain conditions in place of use of if- else statement,
ii). To jump to the end of the loop under certain conditions bypassing the rest of statements inside the loop in place of continue statement,
iii). To jump out of the loop avoiding the use of break statement.

goto can never be used to jump into the loop from outside and it should be preferably used for forward jump.

Situations may arise, however, in which the goto statement can be useful. To the possible extent, the use of the goto statement should generally be avoided.

Program to illustrate goto and label statements

Write a program to print first 10 even numbers

/* Program to print 10 even numbers */

#include <stdio.h>

main()

{

int i=2;

while(1)

{

printf(“%d ”,i);

i=i+2;

if (i>=20)

goto outside;

}

outside : printf(“over”);

}

OUTPUT

2 4 6 8 10 12 14 16 18 20 over

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: