In this article I am going to discuss about C# Goto Statement in detail but before that if you had not read my previous article of C# Continue Statement then must read it for more clear understanding. Goto statement is one of the type of jump statements under control flow statements in csharp.
Introduction to C# Goto Statement
In C#, Goto statement is used to transfer the control to the label in the program. Now question arises in your mind is what is label? The label is type of identifier which is placed before the statement where we want to transfer our control. In C#, Goto Statement is used with keyword goto and with this we can pass the control of execution to the valid identifier called label. A label is any valid identifer which ends with colon. In any programming language when we use goto statement in our program to transfering the control then it is called unstructured control flow statement because it does not follow structured flow of programming.
Syntax of Goto Statement
goto lable;
..
..
label statement:
C# Program to Explain Goto Statement
using System; namespace GotoStatement { class Program { static void Main(string[] args) { Console.WriteLine("Start of Program... "); Console.WriteLine("The First Line of Statement "); goto label1; //label1 is label name Console.WriteLine("The Second Line of Statement "); label1: //label: Console.WriteLine("The Third Line of Statement "); Console.WriteLine("End of Program"); Console.ReadKey(); } } }
Output:
Start of Program...
The First Line of Statement
The Third Line of Statement
End of Program
In above program you can see that how I have used goto statement to transfer the control to label1. With transfering the control ‘The Second Line of Statement’ has not printed in output screen.
Can We Create Multiple Labels inside Our Program
Yes, we can create any number of labels inside our program. When we create multiple labels in our program then control transfer to that label which we specified using goto keyword. Let see an example to understand it more clearly:
using System; namespace GotoStatementWithMultipleLabels { class Program { static void Main(string[] args) { Console.WriteLine("Program Starts... "); Console.WriteLine("The First Line of Statement"); goto label2; //label2 is label name Console.WriteLine("The First Second of Statement"); label1: //label name Console.WriteLine("The First Third of Statement"); label2: //label name Console.WriteLine("The First Fourth of Statement"); Console.WriteLine("Program Ends."); Console.ReadKey(); } } }
Output:
Program Starts...
The First Line of Statement
The First Fourth of Statement
Program Ends.
Goto Statement with Loops in C#
We can also use goto statement with loops in csharp but this is always not recommended. As I told earlier that with using goto statement we disturb the flow of execution therefore it becomes unstructured and this is very bad practice in programming. Therefore it is always recommended to not use goto statement with loops. But if you just want to learn the concept then you can use this in your application. The example of how we can use goto statement with loops is discussed below:
using System; namespace GotoStatementWithLoop { class Program { static void Main(string[] args) { Console.WriteLine("Program starts ..."); for (int i = 1; i <= 5; i++) { if (i == 3) { goto Exitlabel; } Console.WriteLine("The Value of i : " +i); } Exitlabel: Console.WriteLine("The transfer comes to this label"); Console.WriteLine("Program Ends"); Console.ReadKey(); } } }
Output:
Program starts ...
The Value of i : 1
The Value of i : 2
The transfer comes to this label
Program Ends
How to Print 1 to N Without Using any Loop in C#
If you really want to print 1 to N without using any loop in C# then it is possible with goto statement and literally guys this is one of the mostly asked interview questions to print number from 1 to N without using any loop. So lets make a program that print the number from 1 to N.
C# Program to Print 1 to N Without Using any Loop
using System; namespace GotoStatementToPrintNumbers { class Program { static void Main(string[] args) { int count = 1; Console.WriteLine("Enter the Value of N : "); int N=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("The Number will Printed till {0}",N); loopLabel: Console.WriteLine(count); count++; if (count <= N) { goto loopLabel; } Console.ReadKey(); } } }
Output:
Enter the Value of N :
5
The Number will Printed till 5
1
2
3
4
5
Notes:
- It is not always recommended to use goto statement because with using goto statement it is very difficult to find the flow of execution
- With using goto statement our program becomes unstructured. Therefore goto statement is always avoided to use.
- You can also use goto statement inside switch statement but it is not a good practice.
Conclusion
In this C# tutorials article I tried to discuss about goto statement with examples. I hope you like this article about C# goto statement. Now we have discussed three jump statements which is break, continue and goto and two were remaining which is return and throw. I will discuss the return statement when I discuss about functions and throw statement when I discuss about exception handling because if i discuss not then you will not able to understand this. So in my next article I am going to discuss about functions in csharp programming language.