C# Loops

In C#, loops are used to execute a block of code repeatedly as long as a specified condition is satisfied. Loops help reduce code duplication and improve efficiency.

C# provides different types of loops depending on how and when the condition is evaluated.

Types of Loops in C#

  • for loop
  • while loop
  • do-while loop
  • Use foreach for simple and safe iteration over collections

1. for Loop

The for loop is used when the number of iterations is known in advance.


for (initialization; condition; increment/decrement)
{
    // code to be executed
}
    

Example


for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}
    

Use Case: Iterating over arrays, fixed-range loops

2. while Loop

The while loop executes a block of code as long as the condition remains true. The condition is checked before execution.


while (condition)
{
    // code to be executed
}
    

Example


int i = 1;

while (i <= 5)
{
    Console.WriteLine(i);
    i++;
}
    

Note: If the condition is false initially, the loop will not execute even once.

3. do-while Loop

The do-while loop executes the code block at least once before checking the condition.


do
{
    // code to be executed
}
while (condition);
    

Example


int i = 1;

do
{
    Console.WriteLine(i);
    i++;
}
while (i <= 5);
    

Even if the condition is false initially, the loop runs once.

4. foreach Loop

The foreach loop is used to iterate through elements of a collection such as arrays, lists, or other enumerable objects.

It is especially useful when you do not need the index and only want to access each element.


foreach (datatype variable in collection)
{
    // code to be executed
}

Example


string[] fruits = { "Apple", "Banana", "Mango" };

foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

Use Case: Iterating through arrays, lists, and collections without modifying them.

Note: You cannot modify the collection elements directly inside a foreach loop.

Loop Comparison

Loop Condition Check Minimum Execution
for Before loop 0 times
while Before loop 0 times
do-while After loop 1 time
foreach Handled internally 0 times

Common Mistakes

  • Infinite loops due to missing increment/decrement
  • Wrong loop condition
  • Using wrong loop type for the problem

Best Practices

  • Use for when iteration count is known
  • Use while when condition-based looping is needed
  • Use do-while when code must execute at least once
  • Avoid deeply nested loops

Conclusion

Loops are essential for efficient programming in C#. Choosing the correct loop type improves performance and readability.

C# Loops Quiz

Q1. Which loop is best when number of iterations is known?



Q2. Which loop executes at least once?



Q3. When is condition checked in while loop?


C# Introduction C# Variables & Keywords C# Data Types C# If Else Statement C# Loops C# Comments C# Type Casting C# User Input C# Operators C# Math C# String C# OOP Concepts C# Classes and Objects C# Multiple Classes and Objects C# Class Members C# Constructors C# Access Modifiers