Thursday, 29 May 2014

Goto Statement in C

10:53 Posted by Hindustan News , , No comments
In C programming, goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.
Syntax:
goto label;
.............
.............
.............
label: 
statement;
}
 When, the control of program reaches to goto statement, the control of the program will jump to the label: and executes the code below it.


#include
#include
void main(){
    int a;
    goto label;
    a = 10;
    printf(“%d”, a);
    label:
    a = 20;
    printf(“%d”, a);
}
Output:
20

When goto label in C is encountered, control goes to the statement next to the label. Here, label is not the keyword. We can use any name for the label. It’s user defined.
Though goto statement is included in ANSI standard of C, use of goto statement should be reduced as much as possible in a program.

Reasons to avoid goto statement
Though, using goto statement give power to jump to any part of program, using goto statement makes the logic of the program complex and tangled. In modern programming, goto statement is considered a harmful construct and a bad programming practice.
The goto statement can be replaced in most of C program with the use of break and continue statements. In fact, any program in C programming can be perfectly written without the use of goto statement. All programmer should try to avoid goto statement as possible as they can.

0 comments :

Post a Comment