Introduction to Goto Statement in C#
The Goto Statement in C#, also known as the Jump Statement, transfers the program’s flow 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 the case of a deep nested loop, the goto statement can be an excellent 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 midway, we can implement the goto statement and quickly get out of the loop and save time.
Syntax:
Following is the standard syntax for the 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 onto the statement_name part of the program. When any program, whenever or at whatever point, stumbles upon the goto mentioned above 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 the 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 the goto statement, it will check for the 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 Work in C#?
The 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 the Goto Statement in C#.
Example #1
Now that we have understood how Goto Statement works, let’s demonstrate the working of the 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: We use namespace files, then begin our class with the main class within. Then we have our goto keyword, “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. Once the condition meets, the code will proceed to the next statement, which includes our goto statement if someone enters a value other than 18.
As our program touches the goto statement, it’ll jump to the mentioned part, i.e., eligibility, and move ahead from that point. The program will display the message “You are an adult!” to the user if they meet the condition. Meaning the program has concluded. 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 the Goto statement let’s try another example to 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 the Goto Statement. A simple condition can easily check if the input value is above 40. Upon executing, the program will print the first line with “Sorry! you did not pass the test.” Then the program will ask the user to enter a numeric value. The program will enter an IF ELSE loop to check if the entered value is less than or greater than 40 once you input a value. 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 complex. Also, it could not be easy to trace the code flow 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 the Goto Statement. We implemented a Goto statement with two examples with different scenarios. Using the Goto Statement extensively in long programs is not advisable as it can complicate the program’s structure and make it more challenging to debug straightforwardly.
Recommended Articles
This is a guide to Goto Statement in C#. Here we discuss a brief overview of the Goto Statement in C#, its Examples, and its Code Implementation. You can also go through our other suggested articles to learn more –