< Prev
Next >



C - goto Statement





In C language, we may use the goto statement to take the control of the program to almost anywhere in the program(as per our wish). Let us take a look at the syntax of how to declare a goto statement :


goto label ;


Looking at the syntax of the goto statement, the goto statement is defined using the goto keyword and label(separated by a space) of your choice. The label of goto could be defined anywhere in the program using the label name and a semicolon.

When the goto statement is encountered, it takes the control of the program to the place where the label is defined in the program. Hence, goto statement is used to exit/jump from the normal linear flow of execution of the program.



The goto statement example


/* C- The goto statement example */

#include<stdio.h>

int main()
{
int age = 16;

if(age<21)
	goto Under21Team;

else
 printf("Welcome to Senior Team \n");

Under21Team:
 printf("Welcome to Under-21 Team");

return 0;
}/* main method ends and so does the program */


Output


Welcome to Under-21 Team

As you may see in the output of the program, we have defined a goto statement with its label - Under21Team. On encountering this goto statement, the control of the program is taken to the place where its label Under21Team was defined and its associated printf() method is executed.




The label of goto statement is always executed


/* C- The goto statement */

#include<stdio.h>

int main()
{
int age = 23;

if(age<21)
	goto Under21Team;

else
 printf("Welcome to Senior Team \n");

Under21Team:
 printf("Welcome to Under-21 Team");

 
return 0;
}/* main method ends and so does the program */


Output


Enter your age : 23
Welcome to Senior Team
Welcome to Under-21 Team


As you may see in the output of the program, the statement associated with the label of the goto statement is always executed(unless the exit() function is called before it to exit the normal execution of the program). Hence, the printf() statement associated with the label - Under21Team is executed, irrespective of the outcome of the if condition associated with the goto statement.




The goto statement to exit the loop


When the goto statement is encountered within a loop, it skips the execution any statement after it(within the loop) and takes the control right to place where its label is defined, even if it was defined outside the loop.




/* C- goto statement to go out of the loop */

#include<stdio.h>

int main()
{
int counter = 10;

while(counter>=0)
{
 	counter=counter-1;
	printf("Counter : %d \n", counter);

	if(counter==5)
	goto OutOfLoop;
}

OutOfLoop:
	printf("We are out of the loop at the counter 5");

return 0;
} /* main method ends and so does the program */


Output


Counter : 9
Counter : 8
Counter : 7
Counter : 6
Counter : 5
We are out of the loop at the counter 5

As you may see in the output of the program, as soon as the counter reached the value of 5, the if condition got fulfilled and its associated goto statement is executed, which takes the control of the program out of the loop, to the place where the label of this goto statement OutOfLoop is defined.






Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Continue Statemen
Arithmetic Operator in C>

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



Please check our latest addition

PYTHON