Advertisement



< Prev
Next >



C# Goto Statement





Apart from a few looping control structures discussed in our previous tutorials, C# language provides another important loop control statement known as goto, which allows us 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, a goto statement is defined by using the goto keyword, a blank space and a label of our choice, followed by a semicolon. This label of goto statement could be specified anywhere in the program by using the label name with a colon.

When a goto statement is encountered, it takes the control of the program to the place where the label is specified in the program. Hence, a 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

using System;

class A
{
public static void Main()
{
	int age = 16;

	if(age<21)
		goto Under21Team;
	else
		Console.WriteLine("Welcome to Senior Team");
	Console.WriteLine("All the best!");
	Under21Team:
	Console.WriteLine("We are sorry, you are too young to enter our Senior team");

}
}


Output


We are sorry, you are too young to enter our Senior 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 specified/defined(skipping all the statements in between) and, finally the next statement to the label is executed.


Advertisement




The label of goto statement is always executed


// C# The goto statement example

using System;

class A
{
public static void Main()
{
	int age = 25;

	if(age<21)
		goto Under21Team;
	else
		Console.WriteLine("Welcome to Senior Team");
	Console.WriteLine("Do Great!");
	Under21Team:
	Console.WriteLine("The program has ended");

}
}


Output


Welcome to Senior Team
Do Great!
The program has ended


As you may see in the output of the program, the statement associated with the label of a goto statement is always executed(unless the Environment.Exit(0) function is called before it to exit the normal execution of the program). Hence, the Console.WriteLine() 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 

using System;

class A
{
public static void Main()
{
	int counter = 10;
	
	while(counter>=0)
	{
 		counter=counter-1;
		Console.WriteLine("Counter : " + counter);

		if(counter==4)
			goto ExitingLoop;
	}

	ExitingLoop:
		Console.WriteLine("We are out of the loop at the counter 4");
}
}


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 >
< C# Switch Statement
C# Arithmetic Operator



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