The goto Keyword
one of the reasons that programs become unreliable, unreadable, and hard to debug. And yet many programmers find goto seductive.
In a difficult programming situation it seems so easy to use a goto to take the control where you want. However, almost always, there is a more elegant way of writing the same program using if, for, while and switch. These constructs are far more logical and easy to understand.
The big problem with gotos is that when we do use them we can never be sure how we got to a certain point in our code. They obscure the flow of control. So as far as possible skip them. You can always get the job done without them. Trust me, with good programming skills goto can always be avoided.
Example
#include<stdio.h> int main( ) { int per; printf ( "enter the percentage"); scanf ( "%d", &per ); if ( per<=60) goto bye ; else { printf ( "scholarchip given" ) ; printf ( "admission to next class" ) ; } bye : printf ( "goto executed"); getchar(); return 0; }
The only programming situation in favour of using goto is when we want to take the control out of the loop that is contained in several other loops. The following program illustrates this.
Example
#include<stdio.h> int main( ) { int i, j, k ; for ( i = 1 ; i <= 3 ; i++ ) { for ( j = 1 ; j <= 3 ; j++ ) { for ( k = 1 ; k <= 3 ; k++ ) { if ( i == 3 && j == 3 && k == 3 ) goto out ; else printf ( "%d %d %d\n", i, j, k ) ; } } } out : printf ( "Out of the loop at last!" ) ; getchar(); return 0; }