C# Goto & Labels – Easy Guide with Real-World Examples
🚀 C# Goto & Labels – Jump Around Like a Pro!
Hey there! Have you ever wanted to jump to a specific part of your code, just like in video games when you skip to the next level? 🎮 Well, C# lets you do that with Goto & Labels!
But wait! Before you get too excited, let me warn you—using goto
too much can make your code messy. So, let’s learn how to use it wisely!
📌 What You Are Going to Learn in This Lesson
✅ What C# Goto & Labels are and how they work.
✅ How to jump to a different part of the code using goto
.
✅ A real-world example to make it super easy to understand.
✅ When you should and shouldn’t use goto
.
Sounds fun? Let’s jump right in! 😃
What is Goto & Labels in C#?
The goto
statement jumps to a specific part of the code using a label. A label is just a name followed by a colon (:
), like this:
labelName:
So when goto labelName;
is used, the program jumps straight to that label.
Think of it like a shortcut—you can skip some code and go directly where you want!
Example 1: Simple Goto & Labels in C#
using System;
class Program
{
static void Main()
{
Console.WriteLine("Start of the program.");
goto JumpHere; // Jumping to the label below
Console.WriteLine("This line will be skipped! ❌");
JumpHere:
Console.WriteLine("You have jumped here! 🎯");
}
}
Output:
Start of the program.
You have jumped here! 🎯
Explanation:
- The program starts and prints
"Start of the program."
goto JumpHere;
jumps over the nextConsole.WriteLine()
, skipping it.- It lands directly at
JumpHere:
and prints"You have jumped here! 🎯"
This is how C# Goto & Labels work. But let’s make it more interesting!
Real-World Scenario: Retry After Incorrect Input
Imagine you’re building a ticket booking system. If the user enters the wrong choice, you want to ask them again without restarting the entire program.
Here’s how goto
can help:
using System;
class Program
{
static void Main()
{
Retry: // Label
Console.WriteLine("Choose a seat: A, B, or C");
string choice = Console.ReadLine();
if (choice == "A" || choice == "B" || choice == "C")
{
Console.WriteLine($"Seat {choice} booked successfully! ✅");
}
else
{
Console.WriteLine("Invalid choice! Try again. ❌");
goto Retry; // Jump back to Retry label
}
}
}
Output (Example Run):
Choose a seat: A, B, or C
> X
Invalid choice! Try again. ❌
Choose a seat: A, B, or C
> B
Seat B booked successfully! ✅
Explanation:
- The program asks the user to choose a seat.
- If the user enters A, B, or C, it confirms the booking.
- If they enter anything else, the program jumps back to
Retry:
and asks again.
This makes sure the user enters a correct option before moving forward. Cool, right? 😃
When Should You NOT Use Goto? (Very Important! 🚨)
❌ When better alternatives exist! Most of the time, loops
or functions
can do the same thing without making your code messy.
❌ When it makes your code hard to read. Jumping around too much can make debugging a nightmare.
❌ When it causes infinite loops! If there’s no exit condition, your program might keep jumping forever! 😱
🎯 Conclusion
✅ goto
helps you jump to a specific part of your code.
✅ It uses labels (like Retry:
) to know where to jump.
✅ It can be useful in cases like retrying an operation after an error.
✅ But be careful—using goto
too much can make your code harder to understand!
Now it’s your turn! Try using goto
in a small program and see how it works. 😃
👉 Next What?
In the next lesson, we’ll talk about Break & Continue statements in C#! 🚀
Ever wanted to stop a loop immediately or skip a certain iteration? That’s exactly what break
and continue
do! Get ready for some powerful loop control tricks! See you there! 😃