< Prev
Next >



C - goto Statement





C++ language provides another loop control statement known as goto. We may use the goto statement to take the control of the program to almost anywhere in the program(as per our wish). Le't 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<iostream>

int main()
{
int age = 16;

if(age<21)
	goto Under21Team;

else
 cout<<"Welcome to Senior Team \n";

Under21Team:
 count<<"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 cout() method is executed.




The label of goto statement is always executed


// C- The goto statement 

#include<iostream>

int main()
{
int age = 23;

if(age<21)
	goto Under21Team;

else
 cout<<"Welcome to Senior Team \n";

Under21Team:
 count<<"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 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<iostream>

int main()
{
int counter = 10;

while(counter>=0)
{
 	counter=counter-1;
	cout<<"Counter : " << counter <<"\n";

	if(counter==4)
	goto ExitingLoop;
}

ExitingLoop:
	cout<<"We are out of the loop at the counter 4";

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


Output


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

As you may see in the output of the program, as soon as the counter reached the value of 4, 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 ExitingLoop is defined.






Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Continue Statemen
Class in C++
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2