January 2, 2016
How to Break/Exit For Loop in C#/CSharp
To break loop in C#/CSharp, we make use of break statement. Following example demonstrates how you can break or exit for loop in C# when a certain condition is met.
using System;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 100; i++)
{
Console.WriteLine(i);
//Condition to break/exit loop
if (i == 15)
{
break;
}
}
}
}
}
using System;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 100; i++)
{
Console.WriteLine(i);
//Condition to break/exit loop
if (i == 15)
{
break;
}
}
}
}
}
using System; namespace Hello_World { class Program { static void Main(string[] args) { for (int i = 0; i <= 100; i++) { Console.WriteLine(i); //Condition to break/exit loop if (i == 15) { break; } } } } }