Introduction to Goto Statement in C#
The Goto Statement in C#, also known as the Jump Statement, is used to transfer the flow of the program directly to the labeled statement. These statements move the control of the program to other parts. One of the most common applications of the Goto statement is to move the control of the program to a specific point in the switch statements. In case of a deep nested loop, goto statement can be a very good function to get out of the loop. The nested loop continues and the program waits until the end of the loop, but if the condition is satisfied in midway, we can implement goto statement and quickly get out of the loop and save time.
Syntax:
Following is the standard syntax for goto statement:
goto statement_name;
The syntax begins with declaring the goto keyword and then using the statement name. When in the program, whenever this line is to be executed, the program will jump on to the statement_name part of the program. When any program, whenever or at whatever point, stumbles upon the above-mentioned goto syntax, it will execute the goto statement and jump on to the mentioned statement_name and that’s how the control shifts.
Flowchart of Goto Statement
Let us now understand the working of the goto statement in the flowchart.
Referring to the above flowchart, we can understand the code flow of a program with the goto statement. We have multiple statements, 1,2 and 3, and as the code moves ahead, it encounters with goto statement in the 3rd statement. And from 3rd statement, the code will jump to wherever the goto statement is pointing. In our sample, we have our goto statement referring to statement 1. Meaning, when the code stumbles upon goto statement, it will check for condition and based on the result of the condition the code will either move ahead, which means it will conclude the program or the goto statement will be executed and the code will make the jump.
How Goto Statements Works in C#?
Basically Goto statement is a Jump Statement. It works in any program in a way to provide a quick exit. How it works is, To transfer the control of the program to any specific point at any given time, is the primary purpose of Goto Statement in C#.
Example #1
Now that we have understood how Goto Statement works, let’s demonstrate the working of Goto statement with proper code.
Code:
using System;
public class sampleGoto
{
public static void Main(string[] args)
{
eligibility:
Console.WriteLine("You are not an adult yet!");
Console.WriteLine("Enter your age:\n");
int age = Convert.ToInt32(Console.ReadLine());
if (age < 18) {
goto eligibility;
}
else
{
Console.WriteLine("You are an adult!");
Console.Read();
}
}
}
Code Interpretation: Basically we have our using, namespace files. Then beginning of our class with the main class within. Then we have our goto the keyword which goes by as “eligibility” which has a print statement, “You are not an adult yet!”. After printing this statement, the program will move ahead and execute the next line. Here “Enter your age:” is the statement that will be printed and we will have to input a value. Upon entering the value, the program will enter the if statement and check for the condition. If the condition is fulfilled, meaning if the value we entered is else that 18, it will go to the next statement where we have our goto statement. As our program touches the goto statement, it’ll jump to the mentioned part i.e. eligibility and move ahead from that point. Else the program will have it if condition satisfied and will enter the else part where it will print “You are an adult!”, meaning the program has come to a conclusion. Refer below-attached screenshot for output.
As shown in the screenshot, when we passed a value less than 18, it printed the first statement and then when we entered a value greater than 18, the program printed the else statement. Now that we have demonstrated a simple program with Goto statement, let’s try another example which will carry out the same operation.
Example #2
Code:
using System;
public class sampleGoto
{
public static void Main(string[] args)
{
result:
Console.WriteLine("Sorry! you did not pass the test.");
Console.WriteLine("Please submit your Passing Percentage:\n");
int age = Convert.ToInt32(Console.ReadLine());
if (age < 40)
{
goto result;
}
else
{
Console.WriteLine("Congrats! You have passed the Test");
Console.Read();
}
}
}
Code interpretation: Similar to the first program, we have demonstrated the working of Goto Statement. Here we have a simple condition to check if the entered input value is above 40 or not. Upon executing, the program will print the first line with “Sorry! you did not pass the test.” Then the program will ask for the user to enter a numeric value. Upon entering the value, the program will enter IF ELSE loop where the entered value will be checked for a condition of beginning lesser or greater than 40. If the entered value is less than 40, the program will execute the goto statement and jump to a labeled statement. And If the entered value is greater than 40, then the program will proceed and enter the else part. In else part, it will print the “Congrats! You have passed the Test” and end.
Refer to the below attached screenshot for proper output.
Should you Implement GOTO: It is advisable not to implement or use goto statements because the program logic will be more complexed. Also, it could be quite difficult to trace the flow of code once the program encounters a goto statement. On the contrary, if you think using Goto will smoothen the flow of the program then you are free to use it. Goto is rarely used.
Conclusion
We have understood what Goto statement in C# is. We’ve broadly understood the working and the syntax for the Goto Statement. Later, with an example, we demonstrated the working of Goto Statement. We implemented a Goto statement with two examples with different scenarios. Though Goto Statement is easy to use, it is not quite recommended to implement it with long programs as Goto statement might jumble the program and make it difficult to debug in a simpler way.
Recommended Articles
This is a guide to Goto Statement in C#. Here we discuss a brief overview on Goto Statement in C# and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –