Goto Statement in C
Goto Statement in C
Goto Statement in C
The goto keyword is followed by a label. When executed, the program control is
redirected to the statement following the label.If the label points to any of the earlier
statements in a code, it constitutes a loop. On the other hand, if the label refers to a
further step, it is equivalent to a Jump.
goto label;
...
...
label: statement;
The label is any valid identifier in C. A label must contain alphanumeric characters
along with the underscore symbol (_). As in case of any identifier, the same label
cannot be specified more than once in a program. It is always followed by a colon (:)
symbol. The statement after this colon is executed when goto redirects the program
here.
https://www.tutorialspoint.com/cprogramming/c_goto_statement.htm 1/6
6/16/24, 12:24 PM Goto Statement in C
Example 1
In the following program, the control jumps to a given label which is after the
current statement. It prints a given number, before printing the end of the program
message. If it is "0", it jumps over to the printf statement, displaying the message.
#include <stdio.h>
if (n == 0)
goto end;
printf("The number is: %d", n);
end:
printf ("End of program");
return 0;
}
Output
https://www.tutorialspoint.com/cprogramming/c_goto_statement.htm 2/6
6/16/24, 12:24 PM Goto Statement in C
End of program
Example 2
Here is a program to check if a given number is even or odd. Observe how we used
the goto statement in this program −
#include <stdio.h>
int i = 11;
if (i % 2 == 0){
EVEN:
printf("The number is even \n");
goto END;
}
else{
ODD:
printf("The number is odd \n");
}
END:
printf("End of program");
return 0;
}
Output
Since the given number is 11, it will produce the following output −
Change the number and check the output for different numbers.
Example 3
https://www.tutorialspoint.com/cprogramming/c_goto_statement.htm 3/6
6/16/24, 12:24 PM Goto Statement in C
#include <stdio.h>
Output
Hello World
How are you?
.......
.......
The program prints the two strings continuously until forcibly stopped.
Example 4
In this program, we have two goto statements. The second goto statement forms a
loop because it makes a backward jump. The other goto statement jumps out of the
loop when the condition is reached.
#include <stdio.h>
int main(){
int i = 0;
START:
i++;
printf("i: %d\n", i);
if (i == 5)
goto END;
goto START;
END:
printf("End of loop");
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_goto_statement.htm 4/6
6/16/24, 12:24 PM Goto Statement in C
Output
i: 1
i: 2
i: 3
i: 4
i: 5
End of loop
Example 5
The goto statement is used here to skip all the values of a looping variable that
matches with that of others. As a result, all the unique combinations of 1, 2 and 3
are obtained.
#include <stdio.h>
int i, j, k;
label2: ;
}
label1: ;
}
}
https://www.tutorialspoint.com/cprogramming/c_goto_statement.htm 5/6
6/16/24, 12:24 PM Goto Statement in C
return 0;
}
Output
123
132
213
231
312
321
Noted computer scientist dsger Dijkstra recommended that goto be removed from
all the programming languages. He observed that if the program control jumps in
the middle of a loop, it may yield unpredictable behaviour. The goto statements can
be used to create programs that have multiple entry and exit points, which can make
it difficult to track the flow of control of the program.
Dijkstra's strong observations against the use of goto statement have been
influential, as many mainstream languages do not support goto statements.
However, it is still available in some languages, such as C and C++.
In general, it is best to avoid using goto statements in C. You can instead effectively
use if-else statements, loops and loop controls, function and subroutine calls, and
try-catch-throw statements. Use goto if and only if these alternatives don’t fulfil the
needs of your algorithm.
https://www.tutorialspoint.com/cprogramming/c_goto_statement.htm 6/6