C# Goto Loop Tutorial | Easy Goto Loop Example in C#
Hey buddy, ever wanted to skip steps? 🚀
Imagine you’re in a queue at a theme park 🎢, and there’s a fast pass that lets you skip ahead. That’s what the goto
statement does in C#! It lets you jump to a different part of the program instead of following the normal flow.
Sounds cool? Well, hold on! While goto
is powerful, it can make code messy if not used properly. But don’t worry—I’ll make sure you understand it in a fun way! 😃
What You Are Going to Learn in This Lesson
✔️ What goto
is and how it works.
✔️ A real-world example to make it super simple.
✔️ The syntax of goto in C#.
✔️ A few C# Goto Loop Examples with complete code and output.
✔️ When you should (and shouldn’t) use goto
.
What is Goto in C#?
The goto
statement allows you to jump to a labeled section in your code. Instead of running code line by line, it skips to another part of the program based on a condition.
It’s like saying, “Hey, forget everything, jump to this part!” 😆
Syntax of Goto in C#
goto labelName; // Jump to a label
labelName:
// Code that runs when the jump happens
How it Works?
1️⃣ goto labelName;
makes the program jump to a specific label.
2️⃣ labelName:
is where the program lands after the jump.
Goto Loop Example C# (Basic One First!)
Let’s start with a simple example before moving to a real-world scenario.
1️⃣ Repeat a Loop Using Goto
using System;
class Program
{
static void Main()
{
int number = 1;
startLoop: // Label
Console.WriteLine($"Number: {number}");
number++;
if (number <= 5)
{
goto startLoop; // Jump back to startLoop
}
Console.WriteLine("Loop finished!");
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Loop finished!
How does this work?
- The program prints
number
, then increments it. - If
number
is ≤ 5, it jumps back tostartLoop
and repeats. - Once
number
is 6, the jump stops, and it prints"Loop finished!"
.
2️⃣ Real-World Example: Vending Machine 🥤
Imagine you’re buying a soda from a vending machine. If you enter an invalid choice, it should ask again until you enter a valid option.
Let’s see how goto
helps!
using System;
class Program
{
static void Main()
{
start: // Label
Console.WriteLine("Choose a drink:");
Console.WriteLine("1. Cola");
Console.WriteLine("2. Orange Juice");
Console.WriteLine("3. Water");
Console.Write("Enter your choice: ");
int choice = Convert.ToInt32(Console.ReadLine());
if (choice == 1)
Console.WriteLine("You selected Cola. Enjoy! 🥤");
else if (choice == 2)
Console.WriteLine("You selected Orange Juice. Refreshing! 🍊");
else if (choice == 3)
Console.WriteLine("You selected Water. Stay hydrated! 💧");
else
{
Console.WriteLine("Invalid choice! Try again.\n");
goto start; // Jump back to the menu
}
Console.WriteLine("Thank you for your purchase! 😊");
}
}
Example Output:
Choose a drink:
1. Cola
2. Orange Juice
3. Water
Enter your choice: 5
Invalid choice! Try again.
Choose a drink:
1. Cola
2. Orange Juice
3. Water
Enter your choice: 2
You selected Orange Juice. Refreshing! 🍊
Thank you for your purchase! 😊
🔹 If the user enters 1, 2, or 3
, it displays the drink.
🔹 If the user enters an invalid number, it jumps back to start
.
🔹 This ensures the user keeps trying until they enter a valid option.
When Should You Use Goto?
✅ Error Handling (e.g., invalid user input).
✅ Breaking out of deeply nested loops (but break
is usually better).
However…
❌ Avoid using it too much! It makes code hard to read.
❌ Instead, use loops and functions when possible.
Conclusion
Alright, my friend! You now understand how the goto
statement works in C#! 🎉
It’s like a shortcut in your code that jumps to another part when needed. But be careful—if you overuse it, your code might become a mess! 😵
Still confused? No worries! Drop a comment below. We’d love to help! 😊
Next What?
Hey, guess what? There’s something even cooler—Recursion in C#! 🤯 It’s when a function calls itself, kind of like an infinite mirror. In the next chapter, we’ll explore recursion with fun examples. Stay tuned! 🚀