Advertisement



< 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<<"The program has ended";

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


Output


The program has ended

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 next cout statement is executed.


Advertisement




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<<"The program has ended";

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


Output


Welcome to Senior Team
The program has ended


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 cout statement next to 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 -





< Prev
Next >
< Continue Statemen
Class in C++



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement