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.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.