Introduction
The goto statement in C# is used to transfer control to a labeled statement within the same function. While it can be useful in certain scenarios, such as breaking out of deeply nested loops or a switch statement, its use is generally discouraged because it can make code difficult to read and maintain.
Syntax
Define a Label
labelName:
// Code to execute
Use goto to Jump to the Label
goto labelName;
Example: Using goto in a Loop
Here’s a complete example demonstrating the use of the goto statement to exit nested loops:
using System;
namespace GotoStatementExample
{
class Program
{
static void Main(string[] args)
{
// Using nested loops to demonstrate goto
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (i == 3 && j == 3)
{
goto ExitLoops; // Exit both loops
}
Console.WriteLine($"i = {i}, j = {j}");
}
}
ExitLoops:
// Code to execute after exiting the loops
Console.WriteLine("Exited the loops.");
Console.WriteLine("Program finished.");
}
}
}
Output
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 0, j = 3
i = 0, j = 4
i = 1, j = 0
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 2, j = 4
i = 3, j = 0
i = 3, j = 1
i = 3, j = 2
Exited the loops.
Program finished.
Explanation
- Nested Loops: The outer loop runs with
ifrom 0 to 4, and the inner loop runs withjfrom 0 to 4. - Condition Check:
if (i == 3 && j == 3)checks if bothiandjare equal to 3. - goto Statement:
goto ExitLoops;transfers control to the labelExitLoops, effectively breaking out of both loops. - Label: The label
ExitLoops:is placed after the loops, and the code following the label is executed after thegotostatement is encountered.
Example: Using goto in a switch Statement
Here’s an example demonstrating the use of the goto statement in a switch statement:
using System;
namespace GotoInSwitchExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number between 1 and 3:");
int number = Convert.ToInt32(Console.ReadLine());
switch (number)
{
case 1:
Console.WriteLine("You entered one.");
break;
case 2:
Console.WriteLine("You entered two.");
goto case 1; // Transfer control to case 1
case 3:
Console.WriteLine("You entered three.");
break;
default:
Console.WriteLine("Invalid number.");
goto case 1; // Transfer control to case 1 for default case
}
// This will always be executed
Console.WriteLine("Program finished.");
}
}
}
Output
For an input of 2:
Enter a number between 1 and 3:
2
You entered two.
You entered one.
Program finished.
For an input of 5:
Enter a number between 1 and 3:
5
Invalid number.
You entered one.
Program finished.
Explanation
- User Input:
int number = Convert.ToInt32(Console.ReadLine());reads the user input and converts it to an integer. - Switch Statement:
switch (number)evaluates the value ofnumber. - Case Statements: Each
caseblock contains a message to be printed and, for certain cases, agotostatement to transfer control to another case. - goto case:
goto case 1;transfers control to thecase 1block, allowing the code incase 1to be executed after the code incase 2or thedefaultcase.
Conclusion
The goto statement in C# allows you to transfer control to a labeled statement within the same function. While it can be useful in certain scenarios, such as breaking out of nested loops or transferring control in a switch statement, its use is generally discouraged because it can lead to code that is difficult to read and maintain. By understanding the proper use cases for goto, you can use it effectively when necessary while avoiding potential pitfalls.