Home » Syntax References

Syntax of goto statement in C/C++ programming language



C/C++ programming language goto statement: what is break, when it is used and how, where t is used? Learn about goto statement with Syntax, Example.

goto is a keyword in C, C++ programming language, it is a basically jumping statement and used to transfer the program’s control anywhere (to a particular label and label can be defined anywhere within a scope) within a scope.

Here is the syntax of goto statement in C/C++:

//any scope
{
	//statement(s)
	[condition]
		goto label_name;
	//statement(s)
	label_name;
	//statement(s)
}

Here, I assumed there is a scope and we have to move program’s control from top to bottom, condition is optional here.

Consider the given example:

#include <stdio.h>

int main()
{
	int num;
	
	printf("Enter a positive integer: ");
	scanf("%d",&num);
	
	//if number is less than 0 then transferring the
	//program control to QUIT
	if(num<0)
		goto QUIT;
	
	printf("Entered number is: %d\n",num);
	
	QUIT: //label
	printf("BYE BYE!!!\n");
	return 0;
}

Output

First run:
Enter a positive integer: 100 
Entered number is: 100
BYE BYE!!!

Second run:
Enter a positive integer: -10 
BYE BYE!!!

Consider the output on “second run”, here the input value is -10 which is less than 0, thus program’s control moved to QUIT without printing the value.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.