C# Break & Continue – Easy Guide with Real-World Examples
🚀 C# Break & Continue – Take Control of Your Loops!
Hey there! Have you ever wanted to stop a loop when a certain condition is met? Or maybe skip just one iteration? Well, that’s exactly what break
and continue
do in C#!
Loops are great, but sometimes you don’t want to go through every single iteration. Instead, you might want to exit early or skip over some steps. That’s where break
and continue
come in handy!
Let’s make them super easy for you to understand! 😃
What You Are Going to Learn in This Lesson
✅ What C# Break & Continue statements do.
✅ How break
stops a loop immediately.
✅ How continue
skips one iteration and moves to the next.
✅ A real-world example to make things fun and relatable.
✅ When to use break
and continue
the right way!
Sounds good? Let’s dive in! 🚀
What is the Break Statement in C#?
The break
statement completely stops a loop when a condition is met. It’s like saying, “That’s it, I’m done! No need to go further!”
Example 1: Using Break in a Loop
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
Console.WriteLine("Breaking the loop at i = " + i);
break; // Stops the loop immediately
}
Console.WriteLine("i = " + i);
}
}
}
Output:
i = 1
i = 2
i = 3
i = 4
Breaking the loop at i = 5
Explanation:
- The loop starts at
i = 1
and keeps running. - When
i == 5
, thebreak
statement stops the loop immediately. - No more numbers after 5 are printed because the loop ends right there!
💡 Think of a game where you lose all lives. The game stops immediately! That’s how break
works. 🎮
What is the Continue Statement in C#?
The continue
statement skips the current iteration and moves to the next one. It’s like saying, “I’ll just skip this one and move on!”
Example 2: Using Continue in a Loop
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
Console.WriteLine("Skipping i = " + i);
continue; // Skips the rest of the loop for i = 3
}
Console.WriteLine("i = " + i);
}
}
}
Output:
i = 1
i = 2
Skipping i = 3
i = 4
i = 5
Explanation:
- The loop starts at
i = 1
. - When
i == 3
, thecontinue
statement skips the print statement and moves to the next iteration. - Everything else runs normally, but
i = 3
is skipped!
💡 Think of a song playlist where you skip a song but keep playing the rest! That’s exactly what continue
does! 🎵
Real-World Example: Skipping Out-of-Stock Items
Imagine you are shopping online, and some items are out of stock. You want to skip those items and only add available ones to your cart.
Here’s how you can do it with continue
:
using System;
class Program
{
static void Main()
{
string[] items = { "Laptop", "Headphones", "Out of Stock", "Mouse", "Keyboard" };
foreach (string item in items)
{
if (item == "Out of Stock")
{
Console.WriteLine("Skipping: " + item);
continue; // Skip this item and move to the next one
}
Console.WriteLine("Adding to cart: " + item);
}
}
}
Output:
Adding to cart: Laptop
Adding to cart: Headphones
Skipping: Out of Stock
Adding to cart: Mouse
Adding to cart: Keyboard
Explanation:
- The loop goes through each item in the list.
- When it finds
"Out of Stock"
, it skips adding it to the cart usingcontinue
. - All other items are added normally!
✅ This makes sure the user doesn’t accidentally try to buy something unavailable! 🛒
When to Use Break & Continue?
✅ Use break
when you want to completely stop the loop.
✅ Use continue
when you just want to skip one iteration.
✅ Don’t overuse them! Sometimes, loops and conditions can do the same job more clearly.
🎯 Conclusion
✅ break
stops a loop when a condition is met.
✅ continue
skips one iteration and moves to the next.
✅ break
is useful when you need to exit early (e.g., game over, search found).
✅ continue
is helpful when you only need to skip certain values (e.g., skipping out-of-stock items).
Now, try it out! Can you write a program that stops a loop at a certain number using break
? Or maybe skips even numbers using continue
? Have fun experimenting! 😃
👉 Next What?
In the next lesson, we’ll dive into Return & Throw statements in C#! 🚀
Ever got stuck wondering how to return values from a function or throw an error when something goes wrong? No worries! We’ll learn it together, break it down into simple steps, and make it super easy for you! So, stay tuned—it’s going to be fun! 😃
And hey, if you have any questions or difficulties, drop a comment! We’ll figure it out together and help you out. We’re always happy to help! 😊