Curriculum
Loops in C# are powerful programming constructs that allow developers to execute a block of code repeatedly until a specified condition is met. Loops in C# help automate repetitive tasks, process large amounts of data, improve code efficiency, and reduce code duplication. Every .NET developer uses Loops in C# extensively while building Console Applications, ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, and Enterprise Software Solutions.
Understanding Loops in C# is essential because many real-world applications require repeated execution of tasks such as displaying records, processing transactions, validating data, generating reports, and handling collections.
Loops in C# are control structures that repeatedly execute a set of instructions as long as a specified condition remains true.
Example:
for(int i = 1; i <= 5; i++)
{
Console.WriteLine("Welcome");
}
Output:
Welcome
Welcome
Welcome
Welcome
Welcome
Without Loops in C#, developers would need to write repetitive code manually.
Loops in C# help developers:
Loops are used in almost every modern software application.
A loop generally contains three parts:
Starting value.
Example:
int i = 1;
Determines whether the loop continues.
Example:
i <= 5
Changes the loop variable.
Example:
i++
These components work together to control loop execution.
C# provides several looping mechanisms.
The for Loop is used when the number of iterations is known.
Syntax:
for(initialization; condition; update)
{
// code
}
Example:
for(int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
Output:
1
2
3
4
5
The for Loop is widely used for counting operations.
Execution steps:
This structure makes the for Loop highly efficient.
The while Loop executes as long as a condition remains true.
Syntax:
while(condition)
{
// code
}
Example:
int i = 1;
while(i <= 5)
{
Console.WriteLine(i);
i++;
}
Output:
1
2
3
4
5
The while Loop is useful when the number of iterations is unknown.
The do-while Loop executes at least once before checking the condition.
Syntax:
do
{
// code
}
while(condition);
Example:
int i = 1;
do
{
Console.WriteLine(i);
i++;
}
while(i <= 5);
Output:
1
2
3
4
5
The do-while Loop guarantees at least one execution.
| Feature | while | do-while |
|---|---|---|
| Condition Check | Before Execution | After Execution |
| Minimum Execution | 0 Times | 1 Time |
| Use Case | Standard Validation | Mandatory Execution |
Understanding this difference helps developers choose the correct loop type.
The foreach Loop is used to iterate through collections and arrays.
Syntax:
foreach(dataType variable in collection)
{
// code
}
Example:
string[] courses =
{
"C#",
".NET",
"SQL"
};
foreach(string course in courses)
{
Console.WriteLine(course);
}
Output:
C#
.NET
SQL
The foreach Loop simplifies collection processing.
Advantages include:
It is commonly used in ASP.NET Core and Entity Framework projects.
An Infinite Loop occurs when the condition never becomes false.
Example:
while(true)
{
Console.WriteLine("Running");
}
This loop runs forever until manually stopped.
Developers should always ensure proper loop termination conditions.
A Nested Loop is a loop inside another loop.
Example:
for(int i = 1; i <= 3; i++)
{
for(int j = 1; j <= 2; j++)
{
Console.WriteLine(i + " " + j);
}
}
Output:
1 1
1 2
2 1
2 2
3 1
3 2
Nested Loops are useful for:
Loop Control Statements alter loop behavior.
The break Statement immediately exits the loop.
Example:
for(int i = 1; i <= 10; i++)
{
if(i == 5)
{
break;
}
Console.WriteLine(i);
}
Output:
1
2
3
4
The loop stops when i becomes 5.
The continue Statement skips the current iteration.
Example:
for(int i = 1; i <= 5; i++)
{
if(i == 3)
{
continue;
}
Console.WriteLine(i);
}
Output:
1
2
4
5
The value 3 is skipped.
for(int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
for(int i = 2; i <= 20; i += 2)
{
Console.WriteLine(i);
}
int sum = 0;
for(int i = 1; i <= 5; i++)
{
sum += i;
}
Console.WriteLine(sum);
Output:
15
foreach(Student student in students)
{
Console.WriteLine(student.Name);
}
foreach(Transaction transaction in transactions)
{
ProcessTransaction(transaction);
}
foreach(Product product in products)
{
Console.WriteLine(product.Name);
}
foreach(Employee employee in employees)
{
Console.WriteLine(employee.Name);
}
Loops in C# are fundamental for handling collections of business data.
Incorrect:
int i = 1;
while(i <= 5)
{
Console.WriteLine(i);
}
This creates an Infinite Loop.
Always verify loop conditions to avoid unexpected behavior.
Too many nested loops reduce readability and performance.
Loops in C# are heavily used in:
A strong understanding of Loops in C# helps developers build scalable and efficient software solutions.
Loops in C# repeatedly execute a block of code until a specified condition is met.
The for Loop is commonly used when the number of iterations is known, while the while Loop is used when iterations depend on a condition.
A do-while Loop executes at least once before evaluating the condition.
A foreach Loop is used to iterate through arrays and collections.
The break Statement immediately terminates loop execution.
Loops help automate repetitive tasks, process data efficiently, and simplify software development.
WhatsApp us