Is `goto` Ever Cleaner than `while` in C# Loops?
Foreign-Radish1641 and other community members analyze whether using goto
in loops can be clearer than traditional while
constructs in C#, sharing practical scenarios, examples, and advice.
Is goto
Ever Cleaner than while
in C# Loops?
Author: Foreign-Radish1641
The Debate: goto
vs while
in Repetitive Actions
This community thread examines whether using a goto
statement is ever preferable or clearer than a traditional while
loop in C#. The discussion is rooted in practical code samples and draws on years of development experience.
Standard Loop Example
The typical way to request input until a valid action is chosen:
while (true) {
Console.WriteLine("choose an action (attack, wait, run):");
string input = Console.ReadLine();
if (input is "attack" or "wait" or "run") {
break;
}
}
More Complex Version (Multiple Branches)
while (true) {
Console.WriteLine("choose an action (attack, wait, run):");
string input = Console.ReadLine();
if (input is "attack") {
Console.WriteLine("you attack");
break;
} else if (input is "wait") {
Console.WriteLine("nothing happened");
} else if (input is "run") {
Console.WriteLine("you run");
break;
}
}
Alternative with goto
ChooseAction:
Console.WriteLine("choose an action (attack, wait, run):");
string input = Console.ReadLine();
if (input is "attack") {
Console.WriteLine("you attack");
} else if (input is "wait") {
Console.WriteLine("nothing happened");
goto ChooseAction;
} else if (input is "run") {
Console.WriteLine("you run");
}
The argument is that goto
can sometimes make loops “explicit” and potentially more readable when the flow is simple.
Community Opinions
-
Stick to
while
loops: Most developers advise sticking with established looping constructs likewhile
, citing that they’re what most readers expect and are easier to reason about, especially in teams. -
goto
has rare use cases: Only usegoto
to break out of complex, deeply nested logic where restructuring would otherwise greatly complicate the code. Even then, alternatives like extracting code into a function (and usingreturn
) are preferred.- Example: “Even then, I think I’ve only had to do that once in 22 years of using C#.”
-
Readability beats “cleanness”: The term “cleaner” is vague—what matters more is readability and maintainability. Indentation and structured blocks let you quickly see scope and control flow.
-
Alternatives to
goto
: Usingcontinue
, introducing clearer loop conditions, or code refactoring to use functions/methods to manage control flow are nearly always better choices. -
Compiler Details: Internally, even
while (true)
can compile down to similar jump instructions, so performance isn’t a deciding factor—it’s all about source code clarity. -
Comic Reference: xkcd 292 is humorously cited regarding dangerous use of
goto
. -
Summary: Use
goto
as a last resort. For almost all everyday repetitive input scenarios,while
orfor
loops are the right, most maintainable choice.
Example: Clean Pattern for User Input
string input = "";
while (input is not "attack" or "run") {
Console.WriteLine("choose an action (attack, wait, run):");
input = Console.ReadLine();
if (input is "attack") {
Console.WriteLine("you attack");
} else if (input is "wait") {
Console.WriteLine("nothing happened");
} else if (input is "run") {
Console.WriteLine("you run");
}
}
Key Lessons
- Use standard loop constructs for clarity and maintainability.
- Resort to
goto
only in rare, justified cases (deeply nested error handling, legacy interop, etc.). - Prefer refactoring code and leveraging functions for managing flow.
- Optimize for team understanding and code review friendliness.
This post appeared first on “Reddit CSharp”. Read the entire article here